我正在尝试制作一个iPhone应用程序,需要用户能够长按地图视图上的某个位置以在那里放置标记。有人知道如何做到这一点吗?
当您在屏幕上长按时,这种行为可以在Apple地图中观察到。它会落下一个标记并显示一个注释,说“已放置标记”。
当您在屏幕上长按时,这种行为可以在Apple地图中观察到。它会落下一个标记并显示一个注释,说“已放置标记”。
add UILongPressGestureRecognizer
to your MapView
var uilgr = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
uilgr.minimumPressDuration = 2.0
map.add (uilgr)
//IOS 9
map.addGestureRecognizer(uilgr)
在长按检测功能上添加注释 - 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)"])
})
}
}
或者您可以添加没有标题的注释:
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)
}
1) 实例化一个 UILongPressGestureRecognizer
并将其添加到 MKMapView
中。
2) 当用户长按后选择器被调用时,使用适当的标题和坐标调用 MKMapView
中的 addAnnotation 方法。
3) 然后确保符合 MKMapViewDelegate
并实现 viewForAnnotation:
,该方法将在您添加注释后立即调用并返回一个 MKPinAnnotationView
。
首先在 viewDidLoad
中声明 UIGestureRecognizer
。
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(addWaypoint(longGesture:)))
mapView.addGestureRecognizer(longGesture)
其次添加长按功能函数
@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(wayAnnotation)
}
我建议创建一个数组来存储注释,以便在需要删除时更加方便,如下所示...
var myAnnotations = [MKPointAnnotation]()
如果您有不同的注释,可以仅删除您想要的注释,方法是在添加新注释时将其添加到数组中。要仅删除一组注释,请执行以下操作:
for dots in myAnnotations{
mapView.removeAnnotation(dots)
}
要删除所有注释,请尝试使用以下方法:
mapView.removeAnnotations(mapView.annotations)
很抱歉的是翻译方面可能有不妥之处...
更新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)
}
map.add(uilgr)
改为map.addGestureRecognizer(uilgr)
。 - Ri_