MyLocationViewController.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2016 Google Inc. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GoogleMapsDemos/Samples/MyLocationViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. @implementation MyLocationViewController {
  18. GMSMapView *_mapView;
  19. BOOL _firstLocationUpdate;
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
  24. longitude:151.2086
  25. zoom:12];
  26. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  27. _mapView.delegate = self;
  28. _mapView.settings.compassButton = YES;
  29. _mapView.settings.myLocationButton = YES;
  30. // Listen to the myLocation property of GMSMapView.
  31. [_mapView addObserver:self
  32. forKeyPath:@"myLocation"
  33. options:NSKeyValueObservingOptionNew
  34. context:NULL];
  35. self.view = _mapView;
  36. // Ask for My Location data after the map has already been added to the UI.
  37. GMSMapView *mapView = _mapView;
  38. dispatch_async(dispatch_get_main_queue(), ^{
  39. mapView.myLocationEnabled = YES;
  40. });
  41. }
  42. - (void)mapView:(GMSMapView *)mapView didTapMyLocation:(CLLocationCoordinate2D)location {
  43. NSString *message = [NSString stringWithFormat:@"My Location Dot Tapped at: [lat: %f, lng: %f]",
  44. location.latitude, location.longitude];
  45. UIAlertController *alertController =
  46. [UIAlertController alertControllerWithTitle:@"Location Tapped"
  47. message:message
  48. preferredStyle:UIAlertControllerStyleAlert];
  49. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
  50. style:UIAlertActionStyleDefault
  51. handler:^(UIAlertAction *action){
  52. }];
  53. [alertController addAction:okAction];
  54. [self presentViewController:alertController animated:YES completion:nil];
  55. }
  56. - (void)dealloc {
  57. [_mapView removeObserver:self
  58. forKeyPath:@"myLocation"
  59. context:NULL];
  60. }
  61. #pragma mark - KVO updates
  62. - (void)observeValueForKeyPath:(NSString *)keyPath
  63. ofObject:(id)object
  64. change:(NSDictionary *)change
  65. context:(void *)context {
  66. if (!_firstLocationUpdate) {
  67. // If the first location update has not yet been received, then jump to that
  68. // location.
  69. _firstLocationUpdate = YES;
  70. CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
  71. _mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
  72. zoom:14];
  73. }
  74. }
  75. @end