gpt4 book ai didi

ios - 拖动时更新 MKPointAnnotation 对象

转载 作者:行者123 更新时间:2023-12-01 18:07:26 25 4
gpt4 key购买 nike

我想知道如何更新我的 annotation我拖动它时的标题,因为所有 annotation属性是get仅。

我尝试过的代码:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {

switch (newState) {
case .starting:
//view.dragState = .dragging
print("dragging.....")
case .ending, .canceling:
// view.dragState = .none
let lat = view.annotation?.coordinate.latitude
let long = view.annotation?.coordinate.longitude
let coordinates = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
print(" pin lat \(lat) long \(long)")

//Error here - title is get only property
view.annotation?.title = "New Location"

default: break
}

}

最佳答案

MKMapViewDelegate的拖动方法中,注释是只读的,因此您可以使用MKMapViewDelegatedidSelectAnnotationView方法,因为在拖动之前它会调用didSelectAnnotationView 方法,用于声明一个 MKPointAnnotation 类型的 selectedAnnotation 实例属性。

var selectedAnnotation: MKPointAnnotation?

现在在 didSelectAnnotationView 方法中使用此属性。

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
selectedAnnotation = view.annotation as? MKPointAnnotation
}

现在要更改标题,请使用此注释

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
switch (newState) {
case .starting:
//view.dragState = .dragging
print("dragging.....")
case .ending, .canceling:
// view.dragState = .none
let lat = view.annotation?.coordinate.latitude
let long = view.annotation?.coordinate.longitude
let coordinates = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
print(" pin lat \(lat) long \(long)")
self. selectedAnnotation?.title = "New Location"
default: break
}

}

关于ios - 拖动时更新 MKPointAnnotation 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38762179/

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