为MkPolyline叠加计算右缩放/跨度

时间:2012-01-26 14:53:59

标签: ios mkmapview

你好试着为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]];

}

有人可以帮助我!?

由于

1 个答案:

答案 0 :(得分:8)

这行代码解决了我的问题:

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