Last active
October 16, 2015 21:07
-
-
Save casspangell/d240e3b029bc013af249 to your computer and use it in GitHub Desktop.
iOS MapKit and Google Map API Geocoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//GOOGLE GEOCODING | |
//Get Address String from Google API CLLocation Lat/Lon | |
-(void) googleReverseGeocode :(CLLocation *)location { | |
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:location.coordinate completionHandler: | |
^(GMSReverseGeocodeResponse *response, NSError *error){ | |
if (error) { | |
NSLog(@"Error %@", error.description); | |
} else { | |
GMSAddress *gmsAddress = response.firstResult; | |
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@", gmsAddress.thoroughfare, gmsAddress.locality, gmsAddress.administrativeArea, gmsAddress.postalCode]; | |
[self.addressField setText:address]; | |
[self.mapDataObj setAddress:address]; | |
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:gmsAddress.coordinate.latitude | |
longitude:gmsAddress.coordinate.longitude | |
zoom:ZOOMLEVEL]; | |
[self.gMapView setCamera:camera]; | |
//Update annotation for coord | |
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(gmsAddress.coordinate.latitude, gmsAddress.coordinate.longitude); | |
[self updateAnnotationCoord:coord]; | |
} | |
}]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//GOOGLE GEOCODING | |
//Get CLLocationCoordinate2D from Address String | |
// You don't need to use GoogleKit, but it was very convenient | |
// Using https://github.com/maxsokolov/GoogleKit | |
// https://cocoapods.org/pods/GoogleKit | |
- (void) googleReverseAddress:(NSString *)address completion:(void (^)(CLLocationCoordinate2D coordinate))completionBlock | |
{ | |
GKGeocoderQuery *query = [GKGeocoderQuery query]; | |
query.key = @"SERVER-API-KEY"; | |
query.address = address; | |
__block CLLocationCoordinate2D coord; | |
// perform query | |
[query lookupLocation:^(NSArray *results, NSError *error) { | |
GKGeocoderPlace *place = [results firstObject]; | |
NSLog(@"place %@",results); | |
coord = place.coordinate; | |
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:coord.latitude | |
longitude:coord.longitude | |
zoom:ZOOMLEVEL]; | |
[self.gMapView setCamera:camera]; | |
completionBlock(coord); | |
}]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//APPLE GEOCODING | |
//Get Address String from CLLocation Lat/Lon | |
- (void)reverseGeocode:(CLLocation *)location { | |
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; | |
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { | |
if (error) { | |
NSLog(@"Error %@", error.description); | |
} else { | |
CLPlacemark *placemark = [placemarks lastObject]; | |
NSString *address = [NSString stringWithFormat:@"%@", ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO)]; | |
[self.addressField setText:[address stringByReplacingOccurrencesOfString:@"United States" withString:@""]]; | |
[self.mapDataObj setAddress:self.addressField.text]; | |
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500); | |
[self.mapView setRegion:viewRegion animated:YES]; | |
} | |
}]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//APPLE GEOCODING | |
//Get CLLocationCoordinate2D from Address String | |
-(void)reverseAddress:(NSString*)address completion:(void (^)(BOOL success))completionBlock { | |
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; | |
__block MKCoordinateRegion region; | |
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { | |
if (error) { | |
NSLog(@"%@", error); | |
} else { | |
CLPlacemark *placemark = [placemarks lastObject]; | |
float spanX = 0.00725; | |
float spanY = 0.00725; | |
region.center.latitude = placemark.location.coordinate.latitude; | |
region.center.longitude = placemark.location.coordinate.longitude; | |
region.span = MKCoordinateSpanMake(spanX, spanY); | |
[self.mapView setRegion:region animated:YES]; | |
if(address != NULL){ | |
[self.addressField setText:address]; | |
} | |
//Add annotation for coord | |
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude); | |
[self updateCoord:coord]; | |
} | |
completionBlock(YES); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment