をマッピングするためにアノテーションを追加しない、私は経度を通過させることによって、他のVCから座標を受信し、そのようなのようなグローバルアレイを通して緯度午前:スウィフトのMapViewは私のMapViewためだから
配列に緯度と経度の追加:
func add(newLocation location_one:[String:Any]) {
let momentaryLat = (location_one["latitude"] as! NSString).doubleValue
let momentaryLong = (location_one["longitude"] as! NSString).doubleValue
let annotation = MKPointAnnotation()
annotation.title = location_one["title"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
map.addAnnotation(annotation)
// self.map.centerCoordinate = annotation.coordinate
}
viewForannotationコード:
MapViewVC上の座標を受信します
func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "pinAnnotation"
var annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
}
annotationView?.annotation = annotation
return annotationView
}
座標が正しく転送されており、func addが呼び出されています(break文を使用して検証されています)。ただし、シミュレータのMapViewに移動するとアノテーションが配置されません。テストされている別のAppでこれが正しく動作していたため、何が起こっているのか分かりませんが、何が原因か分かりません。
私はまた、ユーザーの現在の場所を見つけて、それに基づいてピンを配置するコードを持っています。おそらく、その座標コードを追加すると混乱する可能性がありますか?
万一、マップオブジェクトにデリゲートを設定し、viewForAnnotationメソッドを実装しましたか? – apineda
@Rob momentary瞬間的に正しく座標を受け取って、その部分が良いと思います。 viewForAnnotationを含めるように質問を編集しました。私のアプリは、現在の場所の注釈を表示することで正しく機能したので、正しく実装されているかどうかはわかりません。 – Kevin
@apineda私は自己のviewDidLoad()でself.map.delegate = selfを設定します。また、viewForAnnotationメソッドを含めるように質問を編集しましたが、正しく実装しているかどうかはわかりません。 – Kevin