2017-02-15 5 views
0

アプリケーションのダウンロード 'moreViewController'のサーバーからの緯度と経度私はラベルを持っています。ピンをタップすると、このラベルの正しい緯度または経度(および別のビューからイメージ)をどのように変換できますか?MKMapViewDelegate + JSON ALAMOFIRE

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

だから私は今、右のボタンを追加し、次のビューにして

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

     } 
    } 

答えて

1

をセグエそれをダウンロードし、グローバル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) 
    } 

を追加performSegueは、現在のコントローラ参照を渡す代わりに、送信者とともに、選択したビューの注釈を渡します。

prepareForSegue方法アクセスで今すぐ
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    self.performSegue(withIdentifier: "more", sender: view.annotation) 
} 

この送信者のプロパティと、それから、注釈を取得します。

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値が含まれています。

+0

ありがとうございます。 – k0le

+0

@ k0le Welcome mate :) –

+0

申し訳ありませんが、私は見ていない別の質問がありますが、この "場所"はjsonのStringとidの文字列として自分の名前を持っています。 – k0le