在 swift 中通过长按向地图视图添加图钉注释
Adding a pin annotation to a map view on a long press in swift
我正在尝试制作一个 iPhone 应用程序,该应用程序要求用户能够长按地图视图上的某个位置以在该位置放置图钉。有人知道这是怎么做到的吗?
当您长按屏幕时,在苹果地图中可以观察到该行为。它会放下一个图钉并显示一个注释 "dropped pin"
1) 实例化一个UILongPressGestureRecognizer
并将其添加到MKMapView
。
2)当用户长时间按下选择器时,请使用适当的标题和坐标来调用 addannotation方法 in MKMapView
。
3) 然后确保您符合 MKMapViewDelegate
并实施 viewForAnnotation:
,这将在您添加注释和 return 后立即调用 MKPinAnnotationView
将 UILongPressGestureRecognizer
添加到您的 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)
}
更新 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
中声明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)
对翻译表示歉意....
我正在尝试制作一个 iPhone 应用程序,该应用程序要求用户能够长按地图视图上的某个位置以在该位置放置图钉。有人知道这是怎么做到的吗?
当您长按屏幕时,在苹果地图中可以观察到该行为。它会放下一个图钉并显示一个注释 "dropped pin"
1) 实例化一个UILongPressGestureRecognizer
并将其添加到MKMapView
。
2)当用户长时间按下选择器时,请使用适当的标题和坐标来调用 addannotation方法 in MKMapView
。
3) 然后确保您符合 MKMapViewDelegate
并实施 viewForAnnotation:
,这将在您添加注释和 return 后立即调用 MKPinAnnotationView
将
UILongPressGestureRecognizer
添加到您的 MapViewvar 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) }
更新 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
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)
对翻译表示歉意....