0

マップ内のいくつかの場所のズームと中心を修正しようとしています。これは私が使用したコードで、機能しませんでした:

        MKCoordinateRegion region;

        CLLocationDegrees maxLat = -90;
        CLLocationDegrees maxLon = -180;
        CLLocationDegrees minLat = 90;
        CLLocationDegrees minLon = 180;
        for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points
        {
            CLLocation* currentLocation = [arrLocation objectAtIndex:idx];
            if(currentLocation.coordinate.latitude > maxLat)
                maxLat = currentLocation.coordinate.latitude;
            if(currentLocation.coordinate.latitude < minLat)
                minLat = currentLocation.coordinate.latitude;
            if(currentLocation.coordinate.longitude > maxLon)
                maxLon = currentLocation.coordinate.longitude;
            if(currentLocation.coordinate.longitude < minLon)
                minLon = currentLocation.coordinate.longitude;
        }

        region.center.latitude     = (maxLat + minLat) / 2;
        region.center.longitude    = (maxLon + minLon) / 2;
        region.span.latitudeDelta  = (maxLat - minLat) * 2;
        region.span.longitudeDelta = (maxLon - minLon) * 2;

        [mapView setRegion:region animated:YES];

私はそれを試してみましたが、うまくdidAddAnnotationViewsいきdidUpdateUserLocationませんでした。手伝って頂けますか?

4

3 に答える 3

0

このコードを使用

MKMapRect flyTo = MKMapRectNull;
    for (id <MKAnnotation> annotation in self.arrayAnnotation)
    {
        MyLog(@"fly to on");
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo))
        {
            flyTo = pointRect;
        }
        else
        {
            flyTo = MKMapRectUnion(flyTo, pointRect);
            //MyLog(@"else-%@",annotationPoint.x);
        }
    }

    // Position the map so that all overlays and annotations are visible on screen.
    mapView.visibleMapRect = flyTo;

お役に立てば幸いです.....

于 2012-10-04T14:41:03.307 に答える
0
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(theCoordinate, 1000, 1000);
MKCoordinateRegion adjustedRegion = [pointOnMapView regionThatFits:viewRegion];  
[pointOnMapView setRegion:adjustedRegion animated:NO];

pointOnMapView私ですMKMapView

theCoordinateタイプはCLLocationCoordinate2D

于 2012-10-04T16:14:03.860 に答える
0

最後に、次の方法で解決策を見つけました。

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

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

for(id<MKAnnotation> 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;

region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
于 2012-10-05T20:11:58.793 に答える