Mat*_*oon 12 maps annotations mkmapview ios swift
我正在尝试制作一个iPhone应用程序,要求用户能够长时间按下地图视图上的某个位置以放置针.有谁知道这是怎么做的?
当您长按屏幕时,可以在Apple地图中观察到该行为.它将丢弃一个引脚并提供一个注释说"掉针"
itz*_*har 31
添加UILongPressGestureRecognizer到MapView
var uilgr = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
uilgr.minimumPressDuration = 2.0
map.add (uilgr)
//IOS 9
map.addGestureRecognizer(uilgr)
Run Code Online (Sandbox Code Playgroud)在Long press 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)"])
})
}
}
Run Code Online (Sandbox Code Playgroud)或者您可以添加没有任何标题的注释:
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)
}
Run Code Online (Sandbox Code Playgroud)Jos*_*met 11
1)实例化a UILongPressGestureRecognizer并将其添加到MKMapView.
2)当用户长按后调用选择器时,使用适当的标题和坐标调用addAnnotation方法MKMapView.
3)然后确保在添加注释并返回a之后符合MKMapViewDelegate并实现viewForAnnotation:将被调用的权限MKPinAnnotationView
首先UIGestureRecognizer在viewDidLoad
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(addWaypoint(longGesture:)))
mapView.addGestureRecognizer(longGesture)
Run Code Online (Sandbox Code Playgroud)
第二个为longPress添加功能
@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)
}
Run Code Online (Sandbox Code Playgroud)
我建议在数组中创建注释,以便以后删除时为您服务,例如...
var myAnnotations = [CLLocation]()
Run Code Online (Sandbox Code Playgroud)
如果具有不同的注释,则只能删除所需的注释,为此,当您向数组中添加新注释时会添加该注释。要仅删除一组注释,只需执行以下操作
for dots in myAnnotations{
mapView.removeAnnotation(dots)
}
Run Code Online (Sandbox Code Playgroud)
要删除所有注释,请尝试
mapView.removeAnnotations(mapView.annotations)
Run Code Online (Sandbox Code Playgroud)
抱歉,翻译...。
| 归档时间: |
|
| 查看次数: |
20295 次 |
| 最近记录: |