2016-09-19 129 views
3

我有谷歌地圖反向地址解析的問題,當談到設置UILabel的文本到反向地理編碼地址。 UILabel在XIB中用作自定義infowindow。我對其他數據的其他信息窗口定製該工作正常但是,看來,當我嘗試設置反向地理回調/完成處理程序內標籤的文本,這是行不通的。這裏是我到目前爲止的代碼,我已經嘗試過多種方式,包括將它分配給變量,並且我還沒有能夠獲得任何工作。谷歌地圖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 
    } 
} 

並選擇標記時,標籤被設置爲在「用戶數據」屬性通過地址:

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

不幸的是這不會工作。外部函數必須採用座標,並且基本上返回地址。我修改了你提供的開始指向接受座標,但是,當我調用getAddress()時,它會按照預期返回地址,但我仍然無法將UILabel文本設置爲infoWindow中的地址。 – recoilnetworks

+0

發佈您修改的代碼 – Rob

+0

這是正確的答案,非常感謝 – 2017-08-11 21:26:16

1

的第一個反應爲我工作。作爲替代

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

試試這個,它會給你的街道地址:

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

的城市,嘗試

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