2016-11-15 129 views
1

我一直在尋找這個,但無法找到答案,我希望將我的經度和緯度轉換爲純文本地址。因爲我想在標記片段中添加地址。 在此先感謝。如何將谷歌縱橫經緯度轉換爲GoogleMaps中Swift中的地址?

[編輯] 這裏是我的代碼

var Addressxx: String = "" 

let reverseGeoCoder = GMSGeocoder() 

reverseGeoCoder.reverseGeocodeCoordinate(coor, completionHandler: {(placeMark, error) -> Void in 

if error == nil { 

if let placeMarkObject = placeMark { 

if placeMarkObject.results()?.count > 0 { 

Addressxx = (placeMarkObject.firstResult()?.lines)! // Error can't assign [String] to 'String' 
        } else { 
         //Do Nothing 
        } 
       } else { 
        //Do Nothing 
       } 
      } else { 
       print(error?.localizedDescription) 
      } 
     }) 

let time :String = NSLocalizedString("Tracked on: ", comment:"Tracked on: ") + dfmatter.stringFromDate(date) 

let unit = self.speed_unit 

let typeStr = dicz.objectForKey("type") as! String 

if (dicz.allKeys[2] as! String == "battery"){ 

let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit 

let battery :String = NSLocalizedString("battery: ", comment:"battery: ") + (dicz.objectForKey("battery")?.stringValue)! + "%" 

marker.snippet = battery + "\n" + time + speed + "(" + typeStr + ")" 


} else { 

let typeStr = dicz.objectForKey("type") as! String 

let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit + typeStr 

marker.snippet = Addressxx + "\n" + time + "\n" + speed + "\n" + "Type: (" + typeStr + ")" 

} 
+0

你有沒有試過我的答案? –

+0

我嘗試添加它,但它使我無法指定類型[String]的值來鍵入'String'。我將它添加到我的我的變量地址Addressxx =(placeMarkObject.firstResult()?. lines)! –

+0

(placeMarkObject.firstResult()?. lines)!是一個字符串數組。所以你的變量「地址」必須聲明如下。 var address = [String]() –

回答

1

您可以使用反向地理編碼擺脫協調這樣的地址,

let reverseGeoCoder = GMSGeocoder() 
let coordinate = "You coordinate will goes here" 
    reverseGeoCoder.reverseGeocodeCoordinate(coordinate, completionHandler: {(placeMark, error) -> Void in 
     if error == nil { 
      if let placeMarkObject = placeMark { 
       if placeMarkObject.results()?.count > 0 { 
       self.tappingAddress = (placeMarkObject.firstResult()?.lines)! // You can get address here 
       } else { 
       //Do Nothing 
       } 
      } else { 
       //Do Nothing 
      } 
     } else { 
      print(error?.localizedDescription) 
     } 
    }) 

您的修改後的代碼:

let reverseGeoCoder = GMSGeocoder() 

    reverseGeoCoder.reverseGeocodeCoordinate(coor, completionHandler: {(placeMark, error) -> Void in 

     if error == nil { 

      if let placeMarkObject = placeMark { 

       if placeMarkObject.results()?.count > 0 { 

        self.addressArray = (placeMarkObject.firstResult()?.lines)! // Error can't assign [String] to 'String' 
       } else { 
        //Do Nothing 
       } 
      } else { 
       //Do Nothing 
      } 
     } else { 
      print(error?.localizedDescription) 
     } 
    }) 

    let time :String = NSLocalizedString("Tracked on: ", comment:"Tracked on: ") + dfmatter.stringFromDate(date) 

    let unit = self.speed_unit 

    let typeStr = dicz.objectForKey("type") as! String 

    if (dicz.allKeys[2] as! String == "battery"){ 

     let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit 

     let battery :String = NSLocalizedString("battery: ", comment:"battery: ") + (dicz.objectForKey("battery")?.stringValue)! + "%" 

     marker.snippet = battery + "\n" + time + speed + "(" + typeStr + ")" 


    } else { 

     let typeStr = dicz.objectForKey("type") as! String 

     let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit + typeStr 

     for i in addressArray { 
      Addressxx = Addressxx + i + " " 
     } 

     marker.snippet = Addressxx + "\n" + time + "\n" + speed + "\n" + "Type: (" + typeStr + ")" 

    } 

謝謝:)

+0

現在試試看,先生。 –

+0

我一直在索引超出範圍 –

+0

希望一切工作正常? –

相關問題