1

マップビューに3つのピンがある場合、マップが読み込まれたときにこれらのピンにズームするにはどうすればよいですか。つまり、マップがロードされるとき、ズームされたビューが必要ですが、ビューはマップにドロップされたすべてのピンに対応する必要があります。マップにピンが1つしかない場合、その機能。しかし、複数のピンでズームを処理することはできません。必要なヘルプ

4

3 に答える 3

6

以下の方法はそれを行います、

- (void)zoomToFitMapAnnotations:(MKMapView *)mapView { 
    if ([mapView.annotations count] == 0) return; 

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

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

    for(MapAnnotation *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 = [mapView regionThatFits:region]; 
    [mapView setRegion:region animated:YES]; 
}
于 2012-04-19T06:05:31.883 に答える
4

私はアプリで以下のコードを書きました、そしてそれは@ElanthiraiyanSAnswerに似ています

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView insideArray:(NSArray*)anAnnotationArray
{   
    // NSLog(@"%s", __FUNCTION__);
    if([mapView.annotations count] == 0) return;

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

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

for(MKPointAnnotation* annotation in anAnnotationArray)
{
    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 = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];

}

于 2012-04-19T06:13:05.237 に答える
0

すべてのピンの中心を計算できます

- (CLLocationCoordinate2D)centerPointFromAnnotationArray:(NSArray *)array
{
    CLLocationCoordinate2D coorMax = CLLocationCoordinate2DMake(0, 0);
    CLLocationCoordinate2D coorMin = CLLocationCoordinate2DMake(999, 999);

    for ( int i = 0 ; i < [array count] ; ++i )
    {
        LMPoint *a = [array objectAtIndex:i];

        if (  ( a.coordinate.latitude != 0.0f ) || ( a.coordinate.longitude != 0.0f ) )
        {
            if ( a.coordinate.latitude > coorMax.latitude )
            {
                coorMax.latitude = a.coordinate.latitude;
            }
            if ( a.coordinate.longitude > coorMax.longitude )
            {
                coorMax.longitude = a.coordinate.longitude;
            }
            if ( a.coordinate.latitude < coorMin.latitude )
            {
                coorMin.latitude = a.coordinate.latitude;
            }
            if ( a.coordinate.longitude < coorMin.longitude )
            {
                coorMin.longitude = a.coordinate.longitude;
            }
        }
    }

    CLLocationCoordinate2D coorCenter = CLLocationCoordinate2DMake((coorMax.latitude+coorMin.latitude)/2, (coorMax.longitude+coorMin.longitude)/2);

    return coorCenter;
}

次に、適切なズームレベルでポイントをズームします

于 2012-04-19T05:51:08.433 に答える