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

时间:2018-04-03 11:48:26

标签: swift google-maps google-geocoder clgeocoder

我正在尝试将当前的Lat和long转换为当前地址详细信息。所以,我正在使用这个网址来获取谷歌的回复

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

但是只有前两次只有它的回复,一旦我保存了google开发者控制台凭据中的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中获取我当前位置的地址

1 个答案:

答案 0 :(得分:2)

使用以下Swift 3.0代码只需通过调用此函数传递lat long。希望这对您有所帮助。

   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的字符串地址