在一个长按快速的地图视图添加一个引脚注释

我正在试图制作一个iPhone应用程序,要求用户能够长时间按下地图视图上的某个地方以放置一个别针。 有人知道这是怎么做的?

当你长时间按下屏幕时,苹果地图上的行为是可观察的。 它会放下一个别针,并提出一个注释,说“掉销”

1)实例化一个UILongPressGestureRecognizer并将其添加到MKMapView

2)当用户长按之后调用select器时,用适当的标题和坐标调用MKMapViewaddAnnotation方法

3)然后确保你符合MKMapViewDelegate并实现viewForAnnotation:在你添加注释之后viewForAnnotation:它,并返回一个MKPinAnnotationView

  1. UILongPressGestureRecognizer添加到您的MapView

     var uilgr = UILongPressGestureRecognizer(target: self, action: "addAnnotation:") uilgr.minimumPressDuration = 2.0 map.add (uilgr) //IOS 9 map.addGestureRecognizer(uilgr) 
  2. 长按detect – func添加注释:

     func addAnnotation(gestureRecognizer:UIGestureRecognizer){ if gestureRecognizer.state == UIGestureRecognizerState.Began { var touchPoint = gestureRecognizer.locationInView(map) var newCoordinates = map.convertPoint(touchPoint, toCoordinateFromView: map) let annotation = MKPointAnnotation() annotation.coordinate = newCoordinates CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude), completionHandler: {(placemarks, error) -> Void in if error != nil { println("Reverse geocoder failed with error" + error.localizedDescription) return } if placemarks.count > 0 { let pm = placemarks[0] as! CLPlacemark // not all places have thoroughfare & subThoroughfare so validate those values annotation.title = pm.thoroughfare + ", " + pm.subThoroughfare annotation.subtitle = pm.subLocality self.map.addAnnotation(annotation) println(pm) } else { annotation.title = "Unknown Place" self.map.addAnnotation(annotation) println("Problem with the data received from geocoder") } places.append(["name":annotation.title,"latitude":"\(newCoordinates.latitude)","longitude":"\(newCoordinates.longitude)"]) }) } } 
  3. 或者您可以添加没有任何标题的注释:

     func action(gestureRecognizer:UIGestureRecognizer){ var touchPoint = gestureRecognizer.locationInView(map) var newCoordinates = map.convertPoint(touchPoint, toCoordinateFromView: map) let annotation = MKPointAnnotation() annotation.coordinate = newCoordinates map.addAnnotation(annotation) } 

更新Swift3

 func action(gestureRecognizer:UIGestureRecognizer){ let touchPoint = gestureRecognizer.location(in: mapView) let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView) let annotation = MKPointAnnotation() annotation.coordinate = newCoordinates mapView.addAnnotation(annotation) } 

首先在ViewDidLoad中声明UIGesture

 let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(addWaypoint(LongGesture:))) mapView.addGestureRecognizer(longGesture) 

其次为longPress添加function

 @objc func addWaypoint(LongGesture : UIGestureRecognizer) { let touchPoint = LongGesture.location(in: mapView) let wayCoords = mapView.convert(touchPoint, toCoordinateFrom: mapView) let location = CLLocation(latitude: wayCoords.latitude, longitude: wayCoords.longitude) myWaypoints.append(location) let wayAnnotation = MKPointAnnotation() wayAnnotation.coordinate = wayCoords wayAnnotation.title = "waypoint" myAnnotations.append(location) } 

我build议创build一个数组中的注释,如果你想删除它,稍后将为你提供服务,像这样…

 var myAnnotations = [CLLocation]() 

如果您具有不同的注释,则只能删除所需的注释,为此添加新的注释添加到数组。 要只删除一组注释,只需执行以下操作

 for dots in myAnnotations{ mapView.removeAnnotation(dots) } 

要删除所有注释,请尝试

 mapView.removeAnnotations(mapView.annotations) 

道歉的翻译….