1
votes

I would like to know which is best practice for reverse geocoding using Google Maps. I'm consider about API response should be faster and accurate based on the lat and lng for getting the formatted address.

I know these two type methods, in this I want to know which can be more better.

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake([strForLatitude floatValue], [strForLongitude floatValue]) completionHandler:
 ^(GMSReverseGeocodeResponse *response, NSError *error){
     ALog(@"Address: %@ ", response.firstResult);
 }];

OR

NSError *error = nil;
NSString *lookupString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude,longitude];
lookupString = [lookupString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookupString]];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];
NSLog(@"%@",jsonDict);
NSArray* jsonResults = [jsonDict objectForKey:@"results"];

Thanks!

3

3 Answers

2
votes

First one is better because it is using google library via API Key

Second one is not good because the more the API hits with in a time limit, the lesser the chance of getting response because google will block your ip for sometime if you consistently hit that api

In future google may block keyless api support

1
votes

By Using GMSGeocoder is the best one because if google change the URL or the response format which is used in another method then your code will not work properly

So my advice to use Google SDK and its official methods for Geocoder

1
votes

In one of my projects I used first method:

@implementation KSTAddressServiceImplementation

- (void)loadAddressForLatitude:(double)latitude
                     longitude:(double)longitude
                    completion:(void (^)(KSTAddress * _Nullable address, NSError * _Nullable error))completion {
    [[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(latitude, longitude)
                                   completionHandler:^(GMSReverseGeocodeResponse * _Nullable response, NSError * _Nullable error) {
                                       if (completion) {
                                           KSTAddress *address;
                                           if (response.firstResult) {
                                               address = [[KSTAddress alloc] initWithAddress:response.firstResult];
                                           }
                                           completion(address, error);
                                       }
                                   }];
}

@end