Reputation: 5044
I have many coordinates that I am plotting using a MKMapOverlay
and CoreLocation
and I want to be able to contain them all in a view.
How can I contain them all? I thought about taking the halfway point between my first and last coordinate, but if it's not a direct route or if they end up at the same location, then some coordinates may be cut out of the screen.
For example, this would be an example with some points cut out to the bottom right. Note: this is just for example purposes and the path is created with Google Maps, but in the application it will be created by tracking your location history.
Again, if this were on the iPhone, how could I make the map contain all of the points using MKCoordinateRegion
?
Upvotes: 0
Views: 459
Reputation: 1879
try looping through your coordinates and getting the most extreme points (farthest north, south, east, west) for each lat and lon, then take the averages of your extremes. then create a region and set it.
something like
for(int idx = 0; idx < points.count; idx++){
CLLocation* currentLocation = [points objectAtIndex:idx];
if(currentLocation.coordinate.latitude != -180 && currentLocation.coordinate.longitude != -180){
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
}
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = (maxLat - minLat) + .3;
region.span.longitudeDelta = (maxLon - minLon) + .3;
[map regionThatFits:region];
Upvotes: 1
Reputation: 3377
I think you need to loop through all your coordinates and find the Northern, Eastern, Southern and Western bounds. Then you can create a coordinate region based on all values.
Perhaps you could have a look at this question for implementation help?
Upvotes: 1