私はswiftの新人です。3 & Xcode 8です。これは私の初めての質問です。私はMapkitにいくつかのピンを設定したいと思います。また、同じMapkitに同時にユーザーの場所を追加したいと思っています。さらに、私は、このMapkitの中心がユーザーの場所ではなく特定の場所であることを期待しました。しかし、私は問題に直面した。Mapkitの他の場所に青い点でユーザーの場所を表示するにはどうすればいいですか?
Mapkitでこれらのピンを正常に設定できましたが、同じMapkitでユーザーの位置を表示できませんでした。このMapkitの中心としてユーザーの場所を設定しようとすると、ユーザーの場所を正しく表示できます。私のコードは以下の通りです。
import UIKit
import MapKit
import CoreLocation
class MapVC: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var MapView: MKMapView!
let manager = CLLocationManager()
func createSpanAndAnnotation() {
let FirstSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.008, 0.008)
let originalLocation:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 24.942653, longitude: 121.368598)
let EdittedRegion:MKCoordinateRegion = MKCoordinateRegion(center: originalLocation, span: FirstSpan)
MapView.setRegion(EdittedRegion, animated: true)
let TemporaryGarbageCanLatitude = showData().garbageCanLatitudeArray
let TemporaryGarbageCanLongtitude = showData().garbageCanLontitudeArray
let TemporaryGarbageCanDiscription = showData().garbageCanDiscription
// 'showdata' is another class to store my data of garbageCans in another file.
for i in 0...TemporaryGarbageCanLatitude.count-1 {
let garbageCanLocation : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: TemporaryGarbageCanLatitude[i], longitude: TemporaryGarbageCanLongtitude[i])
let pin = PinAnnotation(title: "Free Garbage Can", subtitle: TemporaryGarbageCanDiscription[i], coordinate: garbageCanLocation)
MapView.addAnnotation(pin)
}
}
//-------------------------------------<Above code could work successfully>-------------------------------------
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.MapView.showsUserLocation = true
}
func locationManager2() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
//--------------------------------<It seems that Above code could not work successfully>--------------------------------
override func viewDidLoad() {
super.viewDidLoad()
createSpanAndAnnotation()
locationManager2()
}
}
私はビルドフェーズに「CoreLocation.framework」を追加した、と私はまた、追加した - Info.plistファイル内のプライバシー場所使用中の使用法の説明」を。私はまた、シミュレータを走らせたときの設定でロケーションサービスを有効にしました。さらに、私はアメリカに住んでいなかったので、私は今でも滞在する場所に自分の場所を変更するために、 'シミュレータ>デバッグ>場所>カスタムの場所'に行きました。
しかし、私は上記のことをすべて完了した後でもまだユーザーの場所を表示できませんでした。誰も私がこの問題を解決するのを助けることができますか?もし誰かが私を助けることができれば感謝します。ありがとう!
(私のMapkitのマイスクリーンショットはここですが、私はそれにいくつかの機密情報をぼかす。) simulator screen shot
MapKitのロケーションマネージャは必要ありません。ユーザーの位置を表示するように指示すると、自動的にリクエストされます。次に、isUserLocationVisibleをチェックして実際にそこにあるかどうかを確認します。次に、mapView.userLocationからユーザーの場所を確認して座標を確認します。ユーザを常に追跡したい場合は、userTrackingModeを設定することもできます。 – Brandon
あなたのユーザの場所へのアクセスを要求するには、ある時点で 'CLLocationManager'を使用する必要がありますが、 –
@Brandonあなたの指示に感謝します。私はそれらのことを後で試してみるだろう。 – fafa