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];

我试过了didAddAnnotationViewsdidUpdateUserLocation但没有用。你能帮助我吗?

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 回答