I want to show all the markers that are on my map, after doing some searches I found that it should be done with GMSCoordinateBounds (Google Maps SDK) I\'ve
You could iterate your map markers and get the further point to the east, west, north and south and make [[GMSCoordinateBounds alloc] initWithRegion:]
As per documentation, GMSCoordinateBounds can't be mutated.
GMSCoordinateBounds is immutable and can't be modified after construction.
- (GMSCoordinateBounds *)includingCoordinate:(CLLocationCoordinate2D)coordinate;
- (GMSCoordinateBounds *)includingBounds:(GMSCoordinateBounds *)other;
So we can mutate GMSCoordinateBounds by using above methods includingCoordinate: (for extending coordinates) and includingBounds: (for extending bounds).
So Swift code will looks like below:
var gmsBounds = GMSCoordinateBounds()
for coordinate in coordinates {
let marker = GMSMarker(position: coordinate)
gmsBounds = gmsBounds.includingCoordinate(position)
marker.map = mapView
}
mapView.animate(with: GMSCameraUpdate.fit(gmsBounds, withPadding: 30.0))
Additional Notes:
If you add custom marker view, add padding as width of marker view in addition to required padding.
The easiest way I have found is to create a GMSMutablePath and then add all the coordinates of your markers to it. Then you can use the GMSCoordinateBounds initializer initWithPath: to create the bounds.
Once you have the bounds, you can create the GMSCameraUpdate and use it to animate the map to the visible bounds containing all of your markers.
For example, if you have an array of GMSMarker's:
NSArray *myMarkers; // array of marker which sets in Mapview
GMSMutablePath *path = [GMSMutablePath path];
for (GMSMarker *marker in myMarkers) {
[path addCoordinate: marker.position];
}
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
[_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
As for Google Maps version 2.0.0 if you try to create a GMSCoordinateBounds using the default constructor GMSCoordinateBounds() and you check the "valid" flag it will be returning false and it won't make the animateWithCameraUpdate: move.
Swift 2.3 Solution
if let myLocation = mapView.myLocation {
let path = GMSMutablePath()
path.addCoordinate(myLocation.coordinate)
//add other coordinates
//path.addCoordinate(model.coordinate)
let bounds = GMSCoordinateBounds(path: path)
mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 40))
}
@IBOutlet weak var mapView: GMSMapView!
let camera = GMSCameraPosition.cameraWithLatitude(23.0793, longitude:
72.4957, zoom: 5)
mapView.camera = camera
mapView.delegate = self
mapView.myLocationEnabled = true
*** arry has dictionary object which has value of Latitude and Longitude. ***
let path = GMSMutablePath()
for i in 0..<arry.count {
let dict = arry[i] as! [String:AnyObject]
let latTemp = dict["latitude"] as! Double
let longTemp = dict["longitude"] as! Double
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latTemp, longitude: longTemp)
marker.title = "Austrilia"
marker.appearAnimation = kGMSMarkerAnimationNone
marker.map = self.mapView
path.addCoordinate(CLLocationCoordinate2DMake(latTemp, longTemp))
}
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
Swift 3 - Xcode 8
var bounds = GMSCoordinateBounds()
for marker in yourArrayOfMarkers
{
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fit(bounds, withPadding: 60)
mapView.animate(with: update)