2016-09-19 19 views
3

UILabelのテキストを逆ジオコーディングされた住所に設定する際、Google Maps Reverse Geocodingに問題があります。 UILabelはカスタム情報ウィンドウとしてXIBを使用しています。私は別のカスタム情報を持っていますが、これは正しく動作していますが、リバースジオコードコールバック/補完ハンドラ内でラベルのテキストを設定しようとすると動作しません。ここに私がこれまで持っているコードがあり、私はコードに変数を代入することを含め、複数の方法で試してみました。何も動かすことができませんでした。Google Maps iOS逆ジオコーディング

let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent 
let geocoder = GMSGeocoder() 
let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!, Double(self.locLongitude)!) 

var currentAddress = String() 

geocoder.reverseGeocodeCoordinate(coordinate) { response , error in 
    if let address = response?.firstResult() { 
     let lines = address.lines! as [String] 

     currentAddress = lines.joinWithSeparator("\n") 
    } 
} 

infoWindow.labelAddressStreet.text = currentAddress 

return infoWindow 

私もこれを試してみました:

let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent 
let geocoder = GMSGeocoder() 
let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!, Double(self.locLongitude)!) 

geocoder.reverseGeocodeCoordinate(coordinate) { response , error in 
    if let address = response?.firstResult() { 
     let lines = address.lines! as [String] 

     infoWindow.labelAddressStreet.text = lines.joinWithSeparator("\n") 
    } 
} 

return infoWindow 

すべてが正しく接続されている次のコードは動作しますので:

let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent 
let geocoder = GMSGeocoder() 
let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!, Double(self.locLongitude)!) 

geocoder.reverseGeocodeCoordinate(coordinate) { response , error in 
    if let address = response?.firstResult() { 
     let lines = address.lines! as [String] 
    } 
} 

infoWindow.labelAddressStreet.text = "Unable to reverse geocode location!" 

return infoWindow 

すべてのヘルプは間違いなく感謝されます!

答えて

1

だから関数を呼び出す:あなたはこのような何かを行う必要があります。それはかなりではありませんが、それは動作します。各GMSMarkerには、データを渡すために使用できる「userData」属性があります。私が行ったことは、マーカーの作成を逆ジオコーディング完了ハンドラーに移し、アドレスを「userData」属性に割り当てました。次に、ユーザーがタップして現在のアドレスを表示すると、逆ジオコードがキックオフされ、マーカーが作成されてマップに配置されます。

geocoder.reverseGeocodeCoordinate(position) { response, error in 
    if let location = response?.firstResult() { 
     let marker = GMSMarker(position: position) 
     let lines = location.lines! as [String] 

     marker.userData = lines.joined(separator: "\n") 
     marker.title = lines.joined(separator: "\n") 
     marker.infoWindowAnchor = CGPoint(x: 0.5, y: -0.25) 
     marker.accessibilityLabel = "current" 
     marker.map = self.mapView 

     self.mapView.animate(toLocation: position) 
     self.mapView.selectedMarker = marker 
    } 
} 

とマーカーが選択されている場合、ラベルは「のuserData」属性に渡されたアドレスに設定されています

let infoWindow = Bundle.main.loadNibNamed("InfoWindowCurrent", owner: self, options: nil)?[0] as! InfoWindowCurrent 

infoWindow.labelAddress.text = marker.userData as? String 

return infoWindow 
2

ジオコーディングの中カッコからリターンを使用しています。このため、最後のコードが機能します。 、私は別のルートを行くことになった

func getAddress(currentAdd : (returnAddress :String)->Void){ 

    let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent 
    let geocoder = GMSGeocoder() 
    let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!,Double(self.locLongitude)!) 


    var currentAddress = String() 

    geocoder.reverseGeocodeCoordinate(coordinate) { response , error in 
     if let address = response?.firstResult() { 
      let lines = address.lines! as [String] 

      currentAddress = lines.joinWithSeparator("\n") 

      currentAdd(returnAddress: currentAddress) 
     } 
    }  
} 

そして

getAddress() { (returnAddress) in  

    print("\(returnAddress)") 

    } 
+0

てみてください。これを試して

marker.title = lines.joined(separator: "\n")' 

する代わりに、それはあなたの住所を提供します。外部関数は、座標を取り込んで本質的にアドレスを返す必要があります。しかし、私がgetAddress()を呼び出すと、アドレスが期待通りに返されますが、UILabelテキストをinfoWindowのアドレスとして設定することはできません。 – recoilnetworks

+0

コードを修正した通りに公開 – Rob

+0

これは正解です。ありがとう –

1

最初の応答は私のために働きました。 、都市の

marker.title = response?.firstResult()?.thoroughfare 
marker.snippet = response?.firstResult()?.locality 

残念ながら、これはどちらか動作しません

marker.snippet = response?.firstResult()?.locality 
関連する問題