【问题标题】:How to draw pin annotation on current location, MapKit, Swift, iOS10如何在当前位置、MapKit、Swift、iOS10 上绘制图钉注释
【发布时间】:2016-11-24 08:34:47
【问题描述】:

我在故事板上有一个 MapView 并选中了 User Location 选项。我还编写了一些代码来在地图上绘制另一个注释。此注释 (tappedCoordinates) 的坐标源自已注册的手势。

//add new annotation
let annotation = MKPointAnnotation()
annotation.coordinate = tappedCoordinates
mapView.addAnnotation(annotation)

//add circle radius
let circle = MKCircle(center: tappedCoordinates, radius: 20)
mapView.setRegion(MKCoordinateRegion(center: tappedCoordinates, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)), animated: true)
mapView.add(circle)

此代码允许用户在地图上绘制注释(图钉)。它工作正常,除非用户尝试在其当前位置绘制注释。相反,MapView 认为您选择了当前位置注释,并显示“当前位置”,而不是允许用户绘制自定义注释。

如何阻止用户位置注释被点击,并允许我的自定义注释被放置在同一区域?我不想删除当前位置注释。

【问题讨论】:

  • 您可能将 userInteractionEnabled 设置为 false 以防止触摸它

标签: ios objective-c swift annotations


【解决方案1】:
class ViewController: UIViewController, MKMapViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    // Set map view delegate with controller
    self.mapView.delegate = self
}
}

覆盖其委托方法:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
    return nil
}

if (annotation.isKindOfClass(CustomAnnotation)) {
    let customAnnotation = annotation as? CustomAnnotation
    mapView.translatesAutoresizingMaskIntoConstraints = false
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotation") as MKAnnotationView!

    if (annotationView == nil) {
        annotationView = customAnnotation?.annotationView()
    } else {
        annotationView.annotation = annotation;
    }

    self.addBounceAnimationToView(annotationView)
    return annotationView
} else {
    return nil
}
}

//添加引脚(MKPointAnnotation)

override func viewDidLoad() {
super.viewDidLoad()

// Set map view delegate with controller
self.mapView.delegate = self

let newYorkLocation = CLLocationCoordinate2DMake(40.730872, -74.003066)
// Drop a pin
let dropPin = MKPointAnnotation()
dropPin.coordinate = newYorkLocation
dropPin.title = "USA"
mapView.addAnnotation(dropPin)
}

【讨论】:

    【解决方案2】:
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locObject = locations.last
        let annotation = MKPointAnnotation()
        let centerCoordinate = locObject?.coordinate
        annotation.coordinate = centerCoordinate!
        annotation.title = "Title"
        mapView.addAnnotation(annotation)
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      相关资源
      最近更新 更多