将 LAT/Long 转换为 iOS swift 中的位置地址
convert LAT/Long to Location address in iOS swift
我正在尝试将我当前的经纬度转换为当前的地址详细信息。所以,我正在使用这个 url 来获得来自 google
的响应
https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9892037613554,80.2505832381126&key="MyAPI Key"
但只有前两次只有它给出响应,一旦我在 google 开发者控制台凭据中保存了 api 更改。之后它给出 "REQUEST DENIED"
的状态
{
"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 只需要通过调用这个来传递 lat long function.Hope 这将帮助你。
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
的字符串地址
我正在尝试将我当前的经纬度转换为当前的地址详细信息。所以,我正在使用这个 url 来获得来自 google
的响应https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9892037613554,80.2505832381126&key="MyAPI Key"
但只有前两次只有它给出响应,一旦我在 google 开发者控制台凭据中保存了 api 更改。之后它给出 "REQUEST DENIED"
的状态{
"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 只需要通过调用这个来传递 lat long function.Hope 这将帮助你。
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