我一直在寻找这个,但找不到答案,我希望将我的纬度和经度转换成纯文本地址。因为我想在标记片段中添加地址。 提前谢谢。
[编辑] 这是我的代码
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 :(得分: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 + ")"
}
感谢:)