StructuredGeocoderViewController.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/StructuredGeocoderViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @interface StructuredGeocoderViewController () <GMSMapViewDelegate>
  7. @end
  8. @implementation StructuredGeocoderViewController {
  9. GMSMapView *_mapView;
  10. GMSGeocoder *_geocoder;
  11. }
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
  15. longitude:151.2086
  16. zoom:12];
  17. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  18. _mapView.delegate = self;
  19. _geocoder = [[GMSGeocoder alloc] init];
  20. self.view = _mapView;
  21. }
  22. #pragma mark - GMSMapViewDelegate
  23. - (void)mapView:(GMSMapView *)mapView
  24. didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
  25. // On a long press, reverse geocode this location.
  26. GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
  27. GMSAddress *address = response.firstResult;
  28. if (address) {
  29. NSLog(@"Geocoder result: %@", address);
  30. GMSMarker *marker = [GMSMarker markerWithPosition:address.coordinate];
  31. marker.title = address.thoroughfare;
  32. NSMutableString *snippet = [[NSMutableString alloc] init];
  33. if (address.subLocality != NULL) {
  34. [snippet appendString:[NSString stringWithFormat:@"subLocality: %@\n",
  35. address.subLocality]];
  36. }
  37. if (address.locality != NULL) {
  38. [snippet appendString:[NSString stringWithFormat:@"locality: %@\n",
  39. address.locality]];
  40. }
  41. if (address.administrativeArea != NULL) {
  42. [snippet appendString:[NSString stringWithFormat:@"administrativeArea: %@\n",
  43. address.administrativeArea]];
  44. }
  45. if (address.country != NULL) {
  46. [snippet appendString:[NSString stringWithFormat:@"country: %@\n",
  47. address.country]];
  48. }
  49. marker.snippet = snippet;
  50. marker.appearAnimation = kGMSMarkerAnimationPop;
  51. mapView.selectedMarker = marker;
  52. marker.map = _mapView;
  53. } else {
  54. NSLog(@"Could not reverse geocode point (%f,%f): %@",
  55. coordinate.latitude, coordinate.longitude, error);
  56. }
  57. };
  58. [_geocoder reverseGeocodeCoordinate:coordinate completionHandler:handler];
  59. }
  60. @end