简体   繁体   中英

Only allow one pin drop

I have an app and im following a online course to show how to drop pins on the map but that lets you drop an infinite amount of pins. I only want 1 pin to be dropped at a time, for example if you drop a pin at this location then drop a pin at another it removes original pin

Here is my code so far

let longPress = UILongPressGestureRecognizer(target: self, action: "mapLongPress:")
    longPress.minimumPressDuration = 2
    self.mapView.addGestureRecognizer(longPress)




func mapLongPress(recognizer: UIGestureRecognizer){
    print("its done")

    let touchedAt = recognizer.locationInView(self.mapView)
    let touchedAtCoordinate : CLLocationCoordinate2D = mapView.convertPoint(touchedAt, toCoordinateFromView: self.mapView)
    let newPin = MKPointAnnotation()
    newPin.coordinate = touchedAtCoordinate
    mapView.addAnnotation(newPin)
}

不要每次都创建新的图钉,只需更新图钉的坐标即可

您可以在删除其他图钉之前删除所有注释。

mapView.removeAnnotations(mapView.annotations)

Not sure if this is the correct way but anytime I need to show a new pin or annotation on the map I clear the old one.

[self.mapView removeAnnotations:[self.mapView annotations]];

Sorry havent got the code in swift but it works for me as I need to show vehicle icon on map and every 30 sec it shows the new position. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM