1
我在MKMapView上显示了多个注释,但是当我想用多个注释显示当前用户位置时,它跳转到用户位置并且不显示全部注释和用户位置在同一屏幕上。显示MKMapView以一次显示多个注释和当前用户位置
我在MKMapView上显示了多个注释,但是当我想用多个注释显示当前用户位置时,它跳转到用户位置并且不显示全部注释和用户位置在同一屏幕上。显示MKMapView以一次显示多个注释和当前用户位置
您好,您必须根据地图区域中的注释坐标设置地图视图。
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
if([self.mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(DDAnnotation* annotation in mapView.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
您需要设置根据两个引脚您的MapView的区域 -
float maxLat = current_location_latitude;
float maxLon = current_location_longitude;
float minLat = other_location_latitude;
float minLon = other_location_longitude;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake((maxLat + minLat)/2.0, (maxLon + minLon)/2.0), 1000.0, 1000.0);
region.span.latitudeDelta = MAX(region.span.latitudeDelta, (maxLat - minLat) * 1.20);
region.span.longitudeDelta = MAX(region.span.longitudeDelta, (maxLon - minLon) * 1.20);
[self.mapView setRegion:region animated:YES];
thanx您的帮助 – 2011-12-22 04:22:41