MyLocationViewController.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/MyLocationViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @implementation MyLocationViewController {
  7. GMSMapView *mapView_;
  8. BOOL firstLocationUpdate_;
  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_.settings.compassButton = YES;
  17. mapView_.settings.myLocationButton = YES;
  18. // Listen to the myLocation property of GMSMapView.
  19. [mapView_ addObserver:self
  20. forKeyPath:@"myLocation"
  21. options:NSKeyValueObservingOptionNew
  22. context:NULL];
  23. self.view = mapView_;
  24. // Ask for My Location data after the map has already been added to the UI.
  25. dispatch_async(dispatch_get_main_queue(), ^{
  26. mapView_.myLocationEnabled = YES;
  27. });
  28. }
  29. - (void)dealloc {
  30. [mapView_ removeObserver:self
  31. forKeyPath:@"myLocation"
  32. context:NULL];
  33. }
  34. #pragma mark - KVO updates
  35. - (void)observeValueForKeyPath:(NSString *)keyPath
  36. ofObject:(id)object
  37. change:(NSDictionary *)change
  38. context:(void *)context {
  39. if (!firstLocationUpdate_) {
  40. // If the first location update has not yet been recieved, then jump to that
  41. // location.
  42. firstLocationUpdate_ = YES;
  43. CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
  44. mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
  45. zoom:14];
  46. }
  47. }
  48. @end