在MapView中缩小位置

时间:2012-09-06 08:52:18

标签: iphone cocoa-touch mkmapview

我有 MapView 我在Chennai的区域中添加了5个注释..我想在MapView开始加载后立即缩小Chennai,以便注释明显可见。 在viewDidLoad中:

CLLocationCoordinate2D location6;
    location6.latitude=12.9758;
    location6.longitude=80.2205;
    MapAnnotation *ann6=[[MapAnnotation alloc]initWithTitle:@"Chennai-Velacherry" andCoordinate:location6];
    [mapView addAnnotation:ann6];

     CLLocationCoordinate2D centerlocation;
    centerlocation.longitude=13.0810;
    centerlocation.longitude=80.2740;


    [mapView setCenterCoordinate:centerlocation animated:NO];
     [self.view addSubview:mapView];

4 个答案:

答案 0 :(得分:0)

您无法拥有一组预定义的缩放级别。相反,您可以使用MKMapView设置MKCoordinateRegion的可见区域。示例代码here

答案 1 :(得分:0)

你可以这样做:

创建一个类似的函数:

-(void)zoomToFitMapAnnotations:(MKMapView*)mv {

CLLocationCoordinate2D topLeftCoord;

topLeftCoord.latitude = -90;

topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for(MKAnnotations* annotation in mv.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 = [mv regionThatFits:region];
[mv setRegion:region animated:YES];

}

在绘制完所有引脚后,在viewDidLoad中调用此函数

答案 2 :(得分:0)

试试这个

#define kMAPSPAN 1600

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1*kMAPSPAN, 1*kMAPSPAN);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES]; 

mapView是你的MKMapView对象,kMAPSPAN可以根据你的需要设置,zoomLocation是chennai的coordinate2D位置

答案 3 :(得分:0)

使用此,

        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
        region.center.latitude = [currentPlace.latitude doubleValue];
        region.center.longitude = [currentPlace.longitude doubleValue];
        region.span.longitudeDelta = 0.03f; 
        region.span.latitudeDelta = 0.03f;
        [mapView setRegion:region animated:YES]; 
        [mapView setDelegate:self];
        mapView.zoomEnabled = YES;
        mapView.scrollEnabled = NO;

这些会改变你的缩放级别,

        region.span.longitudeDelta = 0.03f; 
        region.span.latitudeDelta = 0.03f;