CustomMarkersViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/Samples/CustomMarkersViewController.h"
  5. #import <GoogleMaps/GoogleMaps.h>
  6. static int kMarkerCount = 0;
  7. // Returns a random value from 0-1.0f.
  8. static CGFloat randf() {
  9. return (((float)arc4random()/0x100000000)*1.0f);
  10. }
  11. @implementation CustomMarkersViewController {
  12. GMSMapView *_mapView;
  13. }
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969
  17. longitude:144.966085
  18. zoom:4];
  19. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  20. [self addDefaultMarkers];
  21. // Add a button which adds random markers to the map.
  22. UIBarButtonItem *addButton =
  23. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
  24. target:self
  25. action:@selector(didTapAdd)];
  26. addButton.accessibilityLabel = @"Add Markers";
  27. UIBarButtonItem *clearButton =
  28. [[UIBarButtonItem alloc] initWithTitle:@"Clear Markers"
  29. style:UIBarButtonItemStylePlain
  30. target:self
  31. action:@selector(didTapClear)];
  32. self.navigationItem.rightBarButtonItems = @[ addButton, clearButton ];
  33. self.view = _mapView;
  34. }
  35. - (void)addDefaultMarkers {
  36. // Add a custom 'glow' marker around Sydney.
  37. GMSMarker *sydneyMarker = [[GMSMarker alloc] init];
  38. sydneyMarker.title = @"Sydney!";
  39. sydneyMarker.icon = [UIImage imageNamed:@"glow-marker"];
  40. sydneyMarker.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
  41. sydneyMarker.map = _mapView;
  42. // Add a custom 'arrow' marker pointing to Melbourne.
  43. GMSMarker *melbourneMarker = [[GMSMarker alloc] init];
  44. melbourneMarker.title = @"Melbourne!";
  45. melbourneMarker.icon = [UIImage imageNamed:@"arrow"];
  46. melbourneMarker.position = CLLocationCoordinate2DMake(-37.81969, 144.966085);
  47. melbourneMarker.map = _mapView;
  48. }
  49. - (void)didTapAdd {
  50. for (int i = 0; i < 10; ++i) {
  51. // Add a marker every 0.25 seconds for the next ten markers, randomly
  52. // within the bounds of the camera as it is at that point.
  53. double delayInSeconds = (i * 0.25);
  54. dispatch_time_t popTime =
  55. dispatch_time(DISPATCH_TIME_NOW,
  56. (int64_t)(delayInSeconds * NSEC_PER_SEC));
  57. dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
  58. GMSVisibleRegion region = [_mapView.projection visibleRegion];
  59. GMSCoordinateBounds *bounds =
  60. [[GMSCoordinateBounds alloc] initWithRegion:region];
  61. [self addMarkerInBounds:bounds];
  62. });
  63. }
  64. }
  65. - (void)addMarkerInBounds:(GMSCoordinateBounds *)bounds {
  66. CLLocationDegrees latitude = bounds.southWest.latitude +
  67. randf() * (bounds.northEast.latitude - bounds.southWest.latitude);
  68. // If the visible region crosses the antimeridian (the right-most point is
  69. // "smaller" than the left-most point), adjust the longitude accordingly.
  70. BOOL offset = (bounds.northEast.longitude < bounds.southWest.longitude);
  71. CLLocationDegrees longitude = bounds.southWest.longitude + randf() *
  72. (bounds.northEast.longitude - bounds.southWest.longitude + (offset ?
  73. 360 : 0));
  74. if (longitude > 180.f) {
  75. longitude -= 360.f;
  76. }
  77. UIColor *color =
  78. [UIColor colorWithHue:randf() saturation:1.f brightness:1.f alpha:1.0f];
  79. CLLocationCoordinate2D position =
  80. CLLocationCoordinate2DMake(latitude, longitude);
  81. GMSMarker *marker = [GMSMarker markerWithPosition:position];
  82. marker.title = [NSString stringWithFormat:@"Marker #%d", ++kMarkerCount];
  83. marker.appearAnimation = kGMSMarkerAnimationPop;
  84. marker.icon = [GMSMarker markerImageWithColor:color];
  85. marker.rotation = (randf()-0.5f)*20; // rotate between -20 and +20 degrees
  86. marker.map = _mapView;
  87. }
  88. - (void)didTapClear {
  89. [_mapView clear];
  90. [self addDefaultMarkers];
  91. }
  92. @end