gpt4 book ai didi

iphone - MKMapViewDelegate + JSON Alamofire

转载 作者:行者123 更新时间:2023-11-28 15:51:29 25 4
gpt4 key购买 nike

应用程序从我有标签的“moreViewController”上的服务器下载纬度和经度,当我点击图钉时,如何将正确的纬度或经度(以及图像到另一个 View )转换为该标签?

{
"latitude": 40.9233,
"longitude": 20.000,
"image" : "xxxx.xxx/xxx.jpg"
}
{
"latitude": 50.9233,
"longitude": 19.320,
"image" : "xxxx.xxx/yyy.jgp"
}

所以我下载它并保存到全局 var test = [String:AnyObject]()

Alamofire.request("website").responseJSON { response in

print(response.result) // result of response serialization
let json = response.result.val
print("JSON: \(json)"
let jsonTab = json as? Array<Any>
for item in jsonTab as! [AnyObject] {
self.test["lat"] = item["lat"] as? Double! as AnyObject?
self.test["lng"] = item["lng"] as? Double! as AnyObject?
self.addPin(lat: self.test["lat"] as! Double, lng: self.test["lng"] as! Double)

并添加图钉

func addPin(lat:Double, lng:Double) {
let testPin = CLLocationCoordinate2D(latitude: lat, longitude: lng)
let dropPin = MKPointAnnotation()
dropPin.coordinate = testPin
dropPin.title = "test"
mapView.addAnnotation(dropPin)
}

现在添加右键和segue到下一个 View

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
self.performSegue(withIdentifier: "more", sender: self)
}

var annotationIdentifier = ""
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
if view == nil {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
view?.canShowCallout = true
view?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
} else {
view?.annotation = annotation
}
return view
}

var selectedAnnotation: MKPointAnnotation!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.destination is moreViewController {
print("123")
let destViewController:moreViewController = segue.destination as! moreViewController
destViewController.lat = "\(latTest)"

}
}

最佳答案

使用 performSegue 和 sender 而不是传递当前 Controller 引用传递所选 View 的注释。

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
self.performSegue(withIdentifier: "more", sender: view.annotation)
}

现在在 prepareForSegue 方法中访问此 sender 属性并从中获取注释。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.destination is moreViewController {

let destViewController:moreViewController = segue.destination as! moreViewController
if let annotation = sender as? MKAnnotation {
destViewController.lat = "\(annotation.coordinate.latitude)"
destViewController.long = "\(annotation.coordinate.longitude)"
}

}
}

注意:您已经在测试字典中设置了latlong值,所以它会覆盖该值,它只会包含last数组对象的 latlong 值。

关于iphone - MKMapViewDelegate + JSON Alamofire ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250289/

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