獲取你的地址是這樣的: -
func getAddressForLatLng(latitude: CLLocationDegrees, longitude: CLLocationDegrees, completionBlock : ((addrs : String)->Void)) {
let url = NSURL(string: "\(baseUrl)latlng=\(latitude),\(longitude)&key=\(serverKey)")
print(url)
let data = NSData(contentsOfURL: url!)
let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
if let result = json["results"] as? NSArray {
if let address = result[0]["address_components"] as? NSArray {
let number = address[0]["short_name"] as! String
let street = address[1]["short_name"] as! String
let city = address[2]["short_name"] as! String
let state = address[4]["short_name"] as! String
let zip = address[6]["short_name"] as! String
print("\(number) \(street), \(city), \(state) \(zip)")
completionBlock(addrs: "\(number) \(street), \(city), \(state) \(zip)")
}
}
}
更新您的didLongPressAtCoordinate功能: -
func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
self.googleMapsView.clear()
self.googleMapsView.myLocationEnabled = false
marker.map = self.googleMapsView
getAddressForLatLng(coordinate.latitude, longitude: coordinate.longitude) { (addrs) in
marker.title = addrs
marker.snippet = "\(coordinate.latitude),\(coordinate.longitude)"
}
}
記住: - 的baseUrl而serverKey是你的谷歌控制檯的baseUrl和serverKey
嘿scbas1000,歡迎來到Stack Overflow。你的問題非常模糊,幾乎沒有機會被回答。請訪問[如何問]的指南(http://stackoverflow.com/help/how-to-ask)。 – Alexander
感謝您的關注,我編輯它,現在是對的我認爲 – scbas1000
@ scbas1000不,你的問題仍然很模糊,你還沒有發佈任何代碼,顯示你到目前爲止。發佈你的代碼,明確地討論你在一分鐘內的工作,你試圖達到的目標(例子會有所幫助),然後列出你在面對的錯誤/問題 –