swift - 在 iOS swift 中将 LAT/Long 转换为位置地址

标签 swift google-maps google-geocoder clgeocoder

我正在尝试将当前的经纬度转换为当前的地址详细信息。所以,我使用这个 url 从 google 获取响应

https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9892037613554,80.2505832381126&key="MyAPI Key"

但只有前两次只有它给出响应,一旦我在谷歌开发者控制台凭据中保存了 api 更改。之后它给出“请求被拒绝”的状态

{
"error_message": "This IP, site or mobile application is not authorized to use this API key. Request received from IP address "My ip address", with empty referer",
"html_attributions": [],
"results": [],
"status": "REQUEST_DENIED"
} 

我也尝试使用 CLGeocoder。

let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
        if (error != nil){
            print("error in reverseGeocode")
        }
        let placemark = placemarks! as [CLPlacemark]
        if placemark.count>0{
            let placemark = placemarks![0]
            print(placemark.locality!)
            print(placemark.administrativeArea!)
            print(placemark.country!)

            print("\(placemark.locality!), \(placemark.administrativeArea!), \(placemark.country!)")
        }
    }

通过使用地理编码器,有些地方无法正确显示。所以请建议我在 iOS Swift 中获取我当前位置的地址

最佳答案

使用 Swift 3.0 的以下代码只需要通过调用此函数来传递经纬度。希望对您有所帮助。

   func latLong(lat: Double,long: Double)  {

    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: lat , longitude: long)
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        print("Response GeoLocation : \(placemarks)")
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]

        // Country
        if let country = placeMark.addressDictionary!["Country"] as? String {
            print("Country :- \(country)")
            // City
            if let city = placeMark.addressDictionary!["City"] as? String {
                print("City :- \(city)")
                // State
                if let state = placeMark.addressDictionary!["State"] as? String{
                    print("State :- \(state)")
                    // Street
                    if let street = placeMark.addressDictionary!["Street"] as? String{
                        print("Street :- \(street)")
                        let str = street
                        let streetNumber = str.components(
                            separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
                        print("streetNumber :- \(streetNumber)" as Any)

                        // ZIP
                        if let zip = placeMark.addressDictionary!["ZIP"] as? String{
                            print("ZIP :- \(zip)")
                            // Location name
                            if let locationName = placeMark?.addressDictionary?["Name"] as? String {
                                print("Location Name :- \(locationName)")
                                // Street address
                                if let thoroughfare = placeMark?.addressDictionary!["Thoroughfare"] as? NSString {
                                print("Thoroughfare :- \(thoroughfare)")

                                }
                            }
                        }
                    }
                }
            }
        }
    })
}

或者

 let geocoder = CLGeocoder()
                geocoder.geocodeAddressString(addressSingle!, completionHandler: {(placemarks, error) -> Void in
                    if((error) != nil){
                        print("Error", error ?? "")
                    }
                    if let placemark = placemarks?.first {
                        let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
                        print("Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)")

                        let position = CLLocationCoordinate2DMake(coordinates.latitude,coordinates.longitude)
                        let marker = GMSMarker(position: position)

                        marker.snippet = addressSingle
                        marker.map = self.viewMap
                        let camera = GMSCameraPosition.camera(withLatitude: coordinates.latitude, longitude: coordinates.longitude, zoom: 18)
                        self.viewMap?.animate(to: camera)

                        // Change color of marker
                        marker.icon = GMSMarker.markerImage(with: .red)
                    }
                })

其中 addressSingle 是来自 API 的字符串地址

关于swift - 在 iOS swift 中将 LAT/Long 转换为位置地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49629332/

相关文章:

ios - 在 Swift Xcode 中使用其他文件中的函数

javascript - 如何在 Phonegap/Cordova(iOS 和 Android)的谷歌地图中获得后退按钮

google-maps - 自定义 Google map 信息气泡

ruby-on-rails - Rails - 如何使用地理编码器通过 IP 获取当前位置

android - [安卓][谷歌地图] : Get the Address of location on touch

ios - 页面 View Controller 中的页面突然下拉 Action

ios - Swift 错误 Anyobject 没有名为 'Generator' 的成员

ios - 在顶级 View 中居中 SwiftUI View

javascript - 在 Google Maps API 的标记上显示位置信息

javascript - 未调用地理编码回调