2016-11-24 4 views
1

Ich habe ein MapView auf einem Storyboard mit der Option User Location aktiviert. Ich habe auch einen Code geschrieben, um eine weitere Anmerkung auf die Karte zu zeichnen. Die Koordinaten für diese Anmerkung (tappedCoordinates) werden von einer registrierten Geste abgeleitet.Wie zeichne Pin Annotation am aktuellen Standort, 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) 

Mit diesem Code kann der Benutzer eine Anmerkung (einen Pin) auf die Karte zeichnen. Es funktioniert einwandfrei, außer wenn der Benutzer versucht, die Anmerkung an ihrer aktuellen Position zu zeichnen. Stattdessen denkt die , dass Sie die Annotation "Aktuelle Position" auswählen und "Aktueller Standort" anzeigen, anstatt dem Benutzer das Zeichnen der benutzerdefinierten Annotation zu ermöglichen.

Wie kann ich verhindern, dass die Annotation für den Benutzerstandort angetippt werden kann, und zulassen, dass meine benutzerdefinierte Annotation im selben Bereich gelöscht wird? Ich möchte die aktuelle Standortannotation nicht entfernen.

+0

Sie wahrscheinlich auf false gesetzt werden userInteractionEnabled Note zu verhindern darauf – CZ54

Antwort

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 
} 
} 

seine Delegatmethoden außer Kraft setzen:

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 
} 
} 

// pin (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) 
} 
Verwandte Themen