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
Swift solution using GMSCoordinateBounds()
without path,
var bounds = GMSCoordinateBounds()
for location in locationArray
{
let latitude = location.valueForKey("latitude")
let longitude = location.valueForKey("longitude")
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude:latitude, longitude:longitude)
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 100)
mapView.animateWithCameraUpdate(update)
We can find the same code every where , but make sure that it is in viewDidAppear() method
//bounding to a region of markers
GMSCoordinateBounds *bounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:sourceMarker.position coordinate:destMarker.position];
[_mapView moveCamera:[GMSCameraUpdate fitBounds:bounds withPadding:50.0]];
- (void)focusMapToShowAllMarkers
{
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
for (GMSMarker *marker in <An array of your markers>)
bounds = [bounds includingCoordinate:marker.position];
[<yourMap> animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];
}
UPDATE:
are you sure there is nothing wrong in you array of markers and the coordinates? I've tried this code and is working perfectly. I've put this on the viewDidAppear
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:[[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.33",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.453",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.44",@"latitude",@"21.993",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.635",@"latitude",@"21.553",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.3546",@"latitude",@"21.663",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.6643",@"latitude",@"21.212",@"longitude", nil],
[[NSDictionary alloc]initWithObjectsAndKeys:@"44.63466",@"latitude",@"21.3523",@"longitude", nil],nil];
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
CLLocationCoordinate2D location;
for (NSDictionary *dictionary in array)
{
location.latitude = [dictionary[@"latitude"] floatValue];
location.longitude = [dictionary[@"longitude"] floatValue];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.icon = [UIImage imageNamed:(@"marker.png")];
marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
bounds = [bounds includingCoordinate:marker.position];
marker.title = dictionary[@"type"];
marker.map = mapView_;
}
[mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];
This is my result:
Clean swift 3 version:
let bounds = markers.reduce(GMSCoordinateBounds()) {
$0.includingCoordinate($1.position)
}
mapView.animate(with: .fit(bounds, withPadding: 30.0))