gpt4 book ai didi

swift - 快速点击显示标记地址(街道地址)

转载 作者:行者123 更新时间:2023-11-28 11:05:42 26 4
gpt4 key购买 nike

我在谷歌地图中有一个标记的纬度和经度位置,我想将其转换为 Swift 中的位置名称字符串。做这个的最好方式是什么?我想显示标记的位置地址,但我不知道该怎么做。

这是我用来添加标记并获取经纬度的代码:

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
if counterMarker < 2
{
counterMarker += 1
let marker = GMSMarker(position: coordinate)
marker.appearAnimation = kGMSMarkerAnimationPop

marker.map = mapView
marker.position.latitude = coordinate.latitude
marker.position.longitude = coordinate.longitude

print(marker.position.latitude)
print(marker.position.longitude)


}
}

最佳答案

    func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
if counterMarker < 2
{
counterMarker += 1
let marker = GMSMarker(position: coordinate)
marker.appearAnimation = kGMSMarkerAnimationPop

marker.map = mapView
marker.position.latitude = coordinate.latitude
marker.position.longitude = coordinate.longitude

self.getAddressForLatLng(String(format: "%@",marker.position.latitude), longitude:String(format: "%@",marker.position.longitude)

}
}


func getAddressForLatLng(latitude: String, longitude: String) {
let url = NSURL(string: "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=YOUR-APIKEY")
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("\n\(number) \(street), \(city), \(state) \(zip) \(address)")
}
}
}

关于swift - 快速点击显示标记地址(街道地址),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37804311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com