在swift中长按时将图钉注释添加到地图视图中

Mat*_*oon 12 maps annotations mkmapview ios swift

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

当您长按屏幕时,可以在Apple地图中观察到该行为.它将丢弃一个引脚并提供一个注释说"掉针"

itz*_*har 31

  1. 添加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)
  2. 在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)
  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)
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 在iOS 9中,`map.add(uilgr)`应该是`map.addGestureRecognizer(uilgr)` (9认同)

Jos*_*met 11

1)实例化a UILongPressGestureRecognizer并将其添加到MKMapView.

2)当用户长按后调用选择器时,使用适当的标题和坐标调用addAnnotation方法MKMapView.

3)然后确保在添加注释并返回a之后符合MKMapViewDelegate并实现viewForAnnotation:将被调用的权限MKPinAnnotationView

  • convertPoint(_:toCoordinateFromView :)将允许你在CGPoint选择可从UILongPressGestureRecognizer坐标中的MKMapView获得实际转换 (4认同)
  • 第 2 步:如何找到合适的坐标? (3认同)

osc*_*lon 7

首先UIGestureRecognizerviewDidLoad

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)

抱歉,翻译...。