GeocoderViewController.m 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/GeocoderViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @implementation GeocoderViewController {
  7. GMSMapView *mapView_;
  8. GMSGeocoder *geocoder_;
  9. }
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
  13. longitude:151.2086
  14. zoom:12];
  15. mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  16. mapView_.delegate = self;
  17. geocoder_ = [[GMSGeocoder alloc] init];
  18. self.view = mapView_;
  19. }
  20. - (void)mapView:(GMSMapView *)mapView
  21. didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
  22. // On a long press, reverse geocode this location.
  23. GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
  24. GMSAddress *address = response.firstResult;
  25. if (address) {
  26. NSLog(@"Geocoder result: %@", address);
  27. GMSMarker *marker = [GMSMarker markerWithPosition:address.coordinate];
  28. marker.title = [[address lines] firstObject];
  29. if ([[address lines] count] > 1) {
  30. marker.snippet = [[address lines] objectAtIndex:1];
  31. }
  32. marker.appearAnimation = kGMSMarkerAnimationPop;
  33. marker.map = mapView_;
  34. } else {
  35. NSLog(@"Could not reverse geocode point (%f,%f): %@",
  36. coordinate.latitude, coordinate.longitude, error);
  37. }
  38. };
  39. [geocoder_ reverseGeocodeCoordinate:coordinate completionHandler:handler];
  40. }
  41. @end