2012-01-26 82 views
3

喜試圖計算一個MkPolyline右變焦/跨度,它工作(),但它不是接近......計算右變焦/跨度MkPolyline覆蓋

這是我的方法:

-(void)showPathForIndex:(int)index{ 

//calculate new region to show on map 
double center_long = 0.0f; 
double center_lat = 0.0f; 
double max_long = 0.0f; 
double min_long = 0.0f; 
double max_lat = 0.0f; 
double min_lat = 0.0f; 

for (CLLocation *cll in [[self.routes objectAtIndex:index]coordinates]) { 

    //Find maximum & minimum value 
    if (cll.coordinate.latitude > max_lat) { 
     max_lat = cll.coordinate.latitude; 
    } 
    if (cll.coordinate.latitude < min_lat){ 
     min_lat = cll.coordinate.latitude; 
    } 
    if (cll.coordinate.longitude > max_long) { 
     max_long = cll.coordinate.longitude; 
    } 
    if (cll.coordinate.longitude < min_long) { 
     min_long = cll.coordinate.longitude; 
    } 
    center_lat = center_lat + cll.coordinate.latitude; 
    center_long = center_long + cll.coordinate.longitude; 
} 

//calculate average long/lat 
center_lat = center_lat/[[[self.routes objectAtIndex:index]coordinates]count]; 
center_long = center_long/[[[self.routes objectAtIndex:index]coordinates]count]; 

CLLocationCoordinate2D coord = {latitude: center_lat, longitude: center_long}; 
MKCoordinateSpan span = MKCoordinateSpanMake(abs(max_lat) + abs(min_lat), abs(max_long) + abs(min_long)); 

MKCoordinateRegion region = {coord, span}; 
[self.parentMapView setRegion:region]; 

//Add the overlay 
[self.parentMapView addOverlay:[[self.routes objectAtIndex:index]polyline]]; 

}

有人可以幫助我!

感謝

+2

如果您在MKPolyline對象,爲什麼不設置地圖視圖的visibleMapRect到折線的boundingMapRect財產? – Anna

+0

謝謝安娜,這是我需要的! – Maxime

回答

8

這行代碼解決我的問題:

[self.parentMapView setVisibleMapRect:[[[self.routes objectAtIndex:index]polyline]boundingMapRect] animated:YES];