FitBoundsViewController.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/FitBoundsViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. @interface FitBoundsViewController () <GMSMapViewDelegate>
  7. @end
  8. @implementation FitBoundsViewController {
  9. GMSMapView *_mapView;
  10. NSMutableArray *_markers;
  11. }
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969
  15. longitude:144.966085
  16. zoom:4];
  17. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  18. _mapView.delegate = self;
  19. self.view = _mapView;
  20. // Add a default marker around Sydney.
  21. GMSMarker *sydneyMarker = [[GMSMarker alloc] init];
  22. sydneyMarker.title = @"Sydney!";
  23. sydneyMarker.icon = [UIImage imageNamed:@"glow-marker"];
  24. sydneyMarker.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
  25. sydneyMarker.map = _mapView;
  26. GMSMarker *anotherSydneyMarker = [[GMSMarker alloc] init];
  27. anotherSydneyMarker.title = @"Sydney 2!";
  28. anotherSydneyMarker.icon = [UIImage imageNamed:@"glow-marker"];
  29. anotherSydneyMarker.position = CLLocationCoordinate2DMake(-33.8683, 149.2086);
  30. anotherSydneyMarker.map = _mapView;
  31. // Create a list of markers, adding the Sydney marker.
  32. _markers = [NSMutableArray arrayWithObject:sydneyMarker];
  33. [_markers addObject:anotherSydneyMarker];
  34. // Create a button that, when pressed, updates the camera to fit the bounds
  35. // of the specified markers.
  36. UIBarButtonItem *fitBoundsButton =
  37. [[UIBarButtonItem alloc] initWithTitle:@"Fit Bounds"
  38. style:UIBarButtonItemStylePlain
  39. target:self
  40. action:@selector(didTapFitBounds)];
  41. self.navigationItem.rightBarButtonItem = fitBoundsButton;
  42. }
  43. - (void)didTapFitBounds {
  44. GMSCoordinateBounds *bounds;
  45. for (GMSMarker *marker in _markers) {
  46. if (bounds == nil) {
  47. bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:marker.position
  48. coordinate:marker.position];
  49. }
  50. bounds = [bounds includingCoordinate:marker.position];
  51. }
  52. GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds
  53. withPadding:50.0f];
  54. [_mapView moveCamera:update];
  55. }
  56. #pragma mark - GMSMapViewDelegate
  57. - (void)mapView:(GMSMapView *)mapView
  58. didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
  59. GMSMarker *marker = [[GMSMarker alloc] init];
  60. marker.title = [NSString stringWithFormat:@"Marker at: %.2f,%.2f",
  61. coordinate.latitude, coordinate.longitude];
  62. marker.position = coordinate;
  63. marker.appearAnimation = kGMSMarkerAnimationPop;
  64. marker.map = _mapView;
  65. // Add the new marker to the list of markers.
  66. [_markers addObject:marker];
  67. }
  68. @end