2016-11-15 86 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

希望一切工作正常? –

相关问题