Reputation: 2057
I am using google map for iOS. I have a set of coordinates. Among them I want to set one of the coordinates as the center of the map, irrespective of the zoom level. I am able to fit all the coordinates in the frame at the same time, using GMSCameraUpdate fitBounds
. But how can I set a particular coordinate as centre all the time and also fit rest of the coordinates in the frame.
What I have tried is to create GMSCoordinateBounds
, included all the coordinates and set fit bounds
[myMapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:20.0f]];
But what I want is the map view should zoom in such a way that, this particular coordinate should be the exact centre while all other coordinates are also included in the frame.
Upvotes: 3
Views: 3062
Reputation: 2283
Can you please see the following code. I hope it will be helpful to you
The intend is to set the boundaries of the map to focus on all the markers.
The ‘GMSMapView‘ class used to have a method ‘markers‘ which returned all the markers, but its now deprecated. So, you will have to keep track of the markers added to the map.
Assuming that you have the following:
GMSMapView *mapView;
NSMutableArray *markers;
and all the markers are add to the NSMutableArray “markers”.
- (void)ShowAllMarkers
{
CLLocationCoordinate2D firstLocation = ((GMSMarker *)markers.firstObject).position;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:firstLocation coordinate:firstLocation];
for (GMSMarker *marker in markers) {
bounds = [bounds includingCoordinate:marker.position];
}
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:50.0f]];
}
Upvotes: 0
Reputation: 3003
All you need is to create right GMSCoordinatedBounds
object.
Say you have a number of points which you need to be fitted by map camera and the center
point.
First you need to find the most far point from the center
. Then calculate an extra point far'
opposite to one you've found. It would look like this
(far)---------(center)---------(far')
To do so just use simple +/-
operations with latitudes and longitudes of far
and center
coordinates.
Far
and far'
will be exact two points the bounds
required.
Upvotes: 0
Reputation: 6454
You need to store all map point in GMSCoordinateBounds using like this
GMSCoordinateBounds* bounds = [[GMSCoordinateBounds alloc] init ];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(26.00, 75.00);
bounds = [bounds includingCoordinate:position];
Store all position using same type code . Adding all GMSCoordinateBounds position use with map
[myMapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
//myMapview = this is object of your google map.
Upvotes: 1