首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapViewDelegate + JSON ALAMOFIRE

MKMapViewDelegate + JSON ALAMOFIRE
EN

Stack Overflow用户
提问于 2017-02-15 13:07:05
回答 1查看 108关注 0票数 0

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

代码语言:javascript
复制
{
  "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]()

代码语言:javascript
复制
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)

加上引脚

代码语言:javascript
复制
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。

代码语言:javascript
复制
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)"

        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-15 13:46:10

使用带有发送方的performSegue,而不是传递当前控制器引用,传递所选视图的注释。

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    self.performSegue(withIdentifier: "more", sender: view.annotation)
}

现在,在prepareForSegue方法中,访问这个发送方属性并从中获取注释。

代码语言:javascript
复制
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值,因此它将重写该值,并且它将只包含最后一个数组对象的latlong值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42250289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档