這裏的東西我使用,可以幫助您。它會給你一個適合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