2017-02-04 11 views
0

アプリに位置検索バーを追加しました。
テーブルビューコントローラは、ユーザーの場所lat/longと宛先(ユーザーが検索バーに入力するアドレス)で構成されます。私の知る限り緯度/経度はplacemark.coordinate内である:destLatdestLonglat/long placemark.coordinate swiftを抽出する方法

placemark.coordinate = CLLocationCoordinate2D(latitude: 45.253519203737767, longitude: -66.070974382763978) 

Iは、2つの変数を持っています。上記の2つの変数にplacemark.coordinateとplaceから緯度と経度を抽出する方法は?

関連コード:

// cache the pin 
    selectedPin = placemark 

    // clear existing pins 
    mapView.removeAnnotations(mapView.annotations) 
    let annotation = MKPointAnnotation() 
    annotation.coordinate = placemark.coordinate 
    annotation.title = placemark.name 
    if let city = placemark.locality, 
     let prov = placemark.administrativeArea { 
     annotation.subtitle = "\(city), \(prov)" 
    } 

    let destination = placemark.coordinate 
    print("Destination coordinates: \(destination)") 
    let name = placemark.name 
    print("Placemark Name: \(name!)") 


    mapView.addAnnotation(annotation) 
    let span = MKCoordinateSpanMake(0.05, 0.05) 
    let region = MKCoordinateRegionMake(placemark.coordinate, span) 
    mapView.setRegion(region, animated: true) 

コンソール出力:

enter image description here

任意の助けを大幅に高く評価されるだろう。

+0

これを見てみると、次のようになります。http://stackoverflow.com/questions/24706885/how-can-i-plot-addresses-in-swift-converting-address-to-longitude-and-latitude –

答えて

1

おそらくあなたはあまりにも難しいと思っていました。あなたは、他のように、これらの変数に値を割り当てることができます。

let (destLat, destLong) = (placemark.coordinate.latitude, placemark.coordinate.longitude) 
1

あなたが宣言してい両方の変数がDoubleのタイプである場合、あなたは、単にCLLocationCoordinate2Dlatitudelongitudeプロパティにアクセスする必要がありますする必要があります。

destLat = destination.latitude 
destLong = destination.longitude 

varのタイプが異なる場合は、単にDoubleに変更してください。

関連する問題