2016-11-24 9 views
1

User Locationオプションがチェックされたストーリーボード上にMapViewがあります。また、マップ上に別の注釈を描画するためのコードを記述しました。この注釈(tappedCoordinates)の座標は、登録されたジェスチャーから導出されます。現在の場所、MapKit、Swift、iOS10にピン注釈を描画する方法

//add new annotation 
let annotation = MKPointAnnotation() 
annotation.coordinate = tappedCoordinates 
mapView.addAnnotation(annotation) 

//add circle radius 
let circle = MKCircle(center: tappedCoordinates, radius: 20) 
mapView.setRegion(MKCoordinateRegion(center: tappedCoordinates, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)), animated: true) 
mapView.add(circle) 

このコードでは、アノテーション(ピン)をマップに描画できます。ユーザーが現在の場所にアノテーションを描画しようとした場合を除いて、正常に動作します。代わりにMapViewは、現在の場所の注釈を選択すると考えて、ユーザーがカスタム注釈を描画するのではなく、「現在の場所」を表示します。

ユーザーの場所の注釈をタップできないようにするにはどうすればいいですか?また、カスタム注釈を同じ領域にドロップできるようにするにはどうすればよいですか?私は現在の位置の注釈を削除したくありません。

+0

あなたはおそらくタッチを防ぐために、falseにuserInteractionEnabled設定することができその上に – CZ54

答えて

0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let locObject = locations.last 
    let annotation = MKPointAnnotation() 
    let centerCoordinate = locObject?.coordinate 
    annotation.coordinate = centerCoordinate! 
    annotation.title = "Title" 
    mapView.addAnnotation(annotation) 

}

0
class ViewController: UIViewController, MKMapViewDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Set map view delegate with controller 
    self.mapView.delegate = self 
} 
} 
そのデリゲートメソッドをオーバーライド

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
if (annotation is MKUserLocation) { 
    return nil 
} 

if (annotation.isKindOfClass(CustomAnnotation)) { 
    let customAnnotation = annotation as? CustomAnnotation 
    mapView.translatesAutoresizingMaskIntoConstraints = false 
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotation") as MKAnnotationView! 

    if (annotationView == nil) { 
     annotationView = customAnnotation?.annotationView() 
    } else { 
     annotationView.annotation = annotation; 
    } 

    self.addBounceAnimationToView(annotationView) 
    return annotationView 
} else { 
    return nil 
} 
} 

//はピンを追加(MKPointAnnotation)

override func viewDidLoad() { 
super.viewDidLoad() 

// Set map view delegate with controller 
self.mapView.delegate = self 

let newYorkLocation = CLLocationCoordinate2DMake(40.730872, -74.003066) 
// Drop a pin 
let dropPin = MKPointAnnotation() 
dropPin.coordinate = newYorkLocation 
dropPin.title = "USA" 
mapView.addAnnotation(dropPin) 
} 
関連する問題