gpt4 book ai didi

ios - 长按快速将图钉注释添加到 map View

转载 作者:IT王子 更新时间:2023-10-29 05:32:25 24 4
gpt4 key购买 nike

我正在尝试制作一个 iPhone 应用程序,该应用程序要求用户能够长按 map View 上的某个位置以在该位置放置一个图钉。有人知道这是怎么做到的吗?

当您长按屏幕时,可以在苹果 map 中观察到该行为。它会放下一个图钉并显示一个注释,上面写着“dropped pin”

最佳答案

  1. UILongPressGestureRecognizer 添加到您的 MapView

    var uilgr = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
    uilgr.minimumPressDuration = 2.0

    map.add (uilgr)

    //IOS 9
    map.addGestureRecognizer(uilgr)
  2. 在长按检测上添加注释 - 功能:

    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)
    }

关于ios - 长按快速将图钉注释添加到 map View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com