FitBoundsViewController.m 3.4 KB

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