Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit bounds for coordinate array with google maps sdk for iOS?

How to fit bounds for coordinate array with google maps sdk for iOS? I need to zoom map for 4 visible markers.

like image 857
Mecid Avatar asked Feb 28 '13 11:02

Mecid


People also ask

Is Google Maps iOS SDK free?

The Maps SDK for iOS uses a pay-as-you-go pricing model.

How do I add a marker to Google Maps on Iphone?

Type the name of a location or address. This displays a list of matching search results from Google Maps below the search bar at the top. Alternatively, you can tap the blue plus (+) icon in the lower-right corner of the map. Then tap Add new point. Drag the marker on the map to where you want to add a marker.


2 Answers

Here's my solution for this problem. Building a GMSCoordinateBounds object by multiple coordinates.

- (void)focusMapToShowAllMarkers {            CLLocationCoordinate2D myLocation = ((GMSMarker *)_markers.firstObject).position;     GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];      for (GMSMarker *marker in _markers)         bounds = [bounds includingCoordinate:marker.position];      [_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:15.0f]]; } 

Updated answer: Since GMSMapView markers property is deprecated, you should save all markers in your own array.

updated swift 3 answer:

    func focusMapToShowAllMarkers() {         let firstLocation = (markers.first as GMSMarker).position         var bounds = GMSCoordinateBoundsWithCoordinate(firstLocation, coordinate: firstLocation)          for marker in markers {             bounds = bounds.includingCoordinate(marker.position)         }         let update = GMSCameraUpdate.fitBounds(bounds, withPadding: CGFloat(15))         self.mapView.animate(cameraUpdate: update)   } 
like image 175
Lirik Avatar answered Sep 21 '22 15:09

Lirik


Swift 3.0 version of Lirik's answer:

func focusMapToShowAllMarkers() {     let myLocation: CLLocationCoordinate2D = self.markers.first!.position     var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: myLocation, coordinate: myLocation)      for marker in self.markers {         bounds = bounds.includingCoordinate(marker.position)         self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))     } } 

And here's my own way:

func focusMapToShowMarkers(markers: [GMSMarker]) {      guard let currentUserLocation = self.locationManager.location?.coordinate else {         return     }      var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: currentUserLocation,                                                           coordinate: currentUserLocation)      _ = markers.map {         bounds = bounds.includingCoordinate($0.position)         self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))     } } 

And you can call my function above like so:

self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])

like image 44
Glenn Posadas Avatar answered Sep 21 '22 15:09

Glenn Posadas