这里的东西我使用,可以帮助您。它会给你一个适合CLLocations数组的MKCoordinateRegion。然后,您可以使用该区域将它传递给的MKMapView setRegion:动画:
// create a region that fill fit all the locations in it
+ (MKCoordinateRegion) getRegionThatFitsLocations:(NSArray *)locations {
// initialize to minimums, maximums
CLLocationDegrees minLatitude = 90;
CLLocationDegrees maxLatitude = -90;
CLLocationDegrees minLongitude = 180;
CLLocationDegrees maxLongitude = -180;
// establish the min and max latitude and longitude
// of all the locations in the array
for (CLLocation *location in locations) {
if (location.coordinate.latitude < minLatitude) {
minLatitude = location.coordinate.latitude;
}
if (location.coordinate.latitude > maxLatitude) {
maxLatitude = location.coordinate.latitude;
}
if (location.coordinate.longitude < minLongitude) {
minLongitude = location.coordinate.longitude;
}
if (location.coordinate.longitude > maxLongitude) {
maxLongitude = location.coordinate.longitude;
}
}
MKCoordinateSpan span;
CLLocationCoordinate2D center;
if ([locations count] > 1) {
// for more than one location, the span is the diff between
// min and max latitude and longitude
span = MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude);
// and the center is the min + the span (width)/2
center.latitude = minLatitude + span.latitudeDelta/2;
center.longitude = minLongitude + span.longitudeDelta/2;
} else {
// for a single location make a fixed size span (pretty close in zoom)
span = MKCoordinateSpanMake(0.01, 0.01);
// and the center equal to the coords of the single point
// which will be the coords of the min (or max) coords
center.latitude = minLatitude;
center.longitude = minLongitude;
}
// create a region from the center and span
return MKCoordinateRegionMake(center, span);
}
正如你可能已经建立,则需要使用的MKMapView和核心定位,做你想做什么。在我的应用程序中,我知道要显示哪些位置,然后使MKMapView足够大以适应所有位置。上述方法将帮助您实现这一点。但是,如果您想获得适合给定地图区域的位置列表,那么您必须或多或少地做出与上述相反的操作。
感谢您的帮助,但我已经在一对夫妇谷歌搜索后看到这些链接。这些教程非常简单,只适用于一个位置。那里有更先进的东西吗? – gravityone 2010-04-02 07:33:40