How to Obtain Country, State, City from Reversegeocodecoordinate

How to obtain country, state, city from reverseGeocodeCoordinate?

The simplest way is to upgrade to Version 1.7 of the Google Maps SDK for iOS (released February 2014).

From the release notes:

GMSGeocoder now provides structured addresses via GMSAddress, deprecating GMSReverseGeocodeResult.

From GMSAddress Class Reference, you can find these properties:

coordinate
Location, or kLocationCoordinate2DInvalid if unknown.

thoroughfare
Street number and name.

locality
Locality or city.

subLocality
Subdivision of locality, district or park.

administrativeArea
Region/State/Administrative area.

postalCode
Postal/Zip code.

country
The country name.

lines
An array of NSString containing formatted lines of the address.

No ISO country code though.

Also note that some properties may return nil.

Here's a full example:

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
NSLog(@"reverse geocoding results:");
for(GMSAddress* addressObj in [response results])
{
NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude);
NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude);
NSLog(@"thoroughfare=%@", addressObj.thoroughfare);
NSLog(@"locality=%@", addressObj.locality);
NSLog(@"subLocality=%@", addressObj.subLocality);
NSLog(@"administrativeArea=%@", addressObj.administrativeArea);
NSLog(@"postalCode=%@", addressObj.postalCode);
NSLog(@"country=%@", addressObj.country);
NSLog(@"lines=%@", addressObj.lines);
}
}];

and its output:

coordinate.latitude=40.437500
coordinate.longitude=-3.681800
thoroughfare=(null)
locality=(null)
subLocality=(null)
administrativeArea=Community of Madrid
postalCode=(null)
country=Spain
lines=(
"",
"Community of Madrid, Spain"
)

Alternatively, you may consider using Reverse Geocoding in the The Google Geocoding API (example).

Swift - Generate an Address Format from Reverse Geocoding

func getAddressFromLatLon(pdblLatitude: String, withLongitude pdblLongitude: String) {
var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
let lat: Double = Double("\(pdblLatitude)")!
//21.228124
let lon: Double = Double("\(pdblLongitude)")!
//72.833770
let ceo: CLGeocoder = CLGeocoder()
center.latitude = lat
center.longitude = lon

let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)

ceo.reverseGeocodeLocation(loc, completionHandler:
{(placemarks, error) in
if (error != nil)
{
print("reverse geodcode fail: \(error!.localizedDescription)")
}
let pm = placemarks! as [CLPlacemark]

if pm.count > 0 {
let pm = placemarks![0]
print(pm.country)
print(pm.locality)
print(pm.subLocality)
print(pm.thoroughfare)
print(pm.postalCode)
print(pm.subThoroughfare)
var addressString : String = ""
if pm.subLocality != nil {
addressString = addressString + pm.subLocality! + ", "
}
if pm.thoroughfare != nil {
addressString = addressString + pm.thoroughfare! + ", "
}
if pm.locality != nil {
addressString = addressString + pm.locality! + ", "
}
if pm.country != nil {
addressString = addressString + pm.country! + ", "
}
if pm.postalCode != nil {
addressString = addressString + pm.postalCode! + " "
}

print(addressString)
}
})

}

How to get the State or Province using Geocoder Php

The solution to this is to call it this way;

$geo->getAdminLevels()->get(1)->getName();

get() in this context takes a value of the index you wish to return.

You might want to check that the getAdminLevels() and get(1) return values if they may sometimes return nulls. You can do this by implementing something like !empty($geo->getAdminLevels())

How to use Google GMSGeocoder on MKMapView?

How to obtain country, state, city from reverseGeocodeCoordinate?
or
http://blog.sallarp.com/ipad-iphone-forward-geocoding-api-google/

take a look at this example hope it can help you.

How to get the latitude & longitude of a touch location on mapView using Google map API in iOS

If you want just to get the coordinate where the user touchs, you don't need the TapGesture. You could use the delegate method - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate; of GMSMapView. Implement that function inside your code:

 -(void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
double latitude = coordinate.latitude;
double longitude = coordinate.longitude;

//DO SOMETHING HERE WITH THAT INFO
}
  • Swift 4
 func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
let lat = coordinate.latitude
let long = coordinate.longitude

print("Latitude: \(lat), Longitude: \(long)")
}

And add this in viewDidLoad after the Map is created:

mapView_.delegate = self;

The didTapAtCoordinate should be called every time the map is touched after this.

Hope that helps!

get zip code with auto complete api

Using Google Maps iOS SDK Swift:

@IBAction func googleMapsiOSSDKReverseGeocoding(sender: UIButton) {
let aGMSGeocoder: GMSGeocoder = GMSGeocoder()
aGMSGeocoder.reverseGeocodeCoordinate(CLLocationCoordinate2DMake(self.latitude, self.longitude)) {
(let gmsReverseGeocodeResponse: GMSReverseGeocodeResponse!, let error: NSError!) -> Void in

let gmsAddress: GMSAddress = gmsReverseGeocodeResponse.firstResult()
print("\ncoordinate.latitude=\(gmsAddress.coordinate.latitude)")
print("coordinate.longitude=\(gmsAddress.coordinate.longitude)")
print("thoroughfare=\(gmsAddress.thoroughfare)")
print("locality=\(gmsAddress.locality)")
print("subLocality=\(gmsAddress.subLocality)")
print("administrativeArea=\(gmsAddress.administrativeArea)")
print("postalCode=\(gmsAddress.postalCode)")
print("country=\(gmsAddress.country)")
print("lines=\(gmsAddress.lines)")
}
}

Using Google Reverse Geocoding Objective-C:

@IBAction func googleMapsWebServiceGeocodingAPI(sender: UIButton) {
self.callGoogleReverseGeocodingWebservice(self.currentUserLocation())
}

// #1 - Get the current user's location (latitude, longitude).
private func currentUserLocation() -> CLLocationCoordinate2D {
// returns current user's location.
}

// #2 - Call Google Reverse Geocoding Web Service using AFNetworking.
private func callGoogleReverseGeocodingWebservice(let userLocation: CLLocationCoordinate2D) {
let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(userLocation.latitude),\(userLocation.longitude)&key=\(self.googleMapsiOSAPIKey)&language=\(self.googleReverseGeocodingWebserviceOutputLanguageCode)&result_type=country"

AFHTTPRequestOperationManager().GET(
url,
parameters: nil,
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
println("GET user's country request succeeded !!!\n")

// The goal here was only for me to get the user's iso country code +
// the user's Country in english language.
if let responseObject: AnyObject = responseObject {
println("responseObject:\n\n\(responseObject)\n\n")
let rootDictionary = responseObject as! NSDictionary
if let results = rootDictionary["results"] as? NSArray {
if let firstResult = results[0] as? NSDictionary {
if let addressComponents = firstResult["address_components"] as? NSArray {
if let firstAddressComponent = addressComponents[0] as? NSDictionary {
if let longName = firstAddressComponent["long_name"] as? String {
println("long_name: \(longName)")
}
if let shortName = firstAddressComponent["short_name"] as? String {
println("short_name: \(shortName)")
}
}
}
}
}
}
},
failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
println("Error GET user's country request: \(error.localizedDescription)\n")
println("Error GET user's country request: \(operation.responseString)\n")
}
)

}

For more detail please check here.



Related Topics



Leave a reply



Submit