0

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?

Baub
  • 4,824
  • 14
  • 52
  • 98
  • This sounds like a [duplicate of this question](http://stackoverflow.com/questions/1336370/positioning-mkmapview-to-show-multiple-annotations-at-once). – Michael Dautermann Nov 07 '11 at 15:26
  • I guess I was searching for the wrong terms. Thank you! – Baub Nov 07 '11 at 15:49
  • You are looking for the [minimum bounding box](http://en.wikipedia.org/wiki/Minimum_bounding_box_algorithms) of your points. See also [convex hull](http://en.wikipedia.org/wiki/Convex_hull_algorithms). – jscs Nov 07 '11 at 18:59

2 Answers2

1

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?

Community
  • 1
  • 1
Jakob W
  • 3,340
  • 17
  • 27
1

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];
AtomRiot
  • 1,829
  • 3
  • 18
  • 23