0

我找到了有关缩放以适应标准尺寸方法的所有注释的主题。我使用了该代码,但问题是地图只有 150 英尺高(但站宽)。我使用的代码一切看起来都很好。但问题是地图上边缘显示的图钉总是不适合地图区域

这是代码。我试图增加填充,但地图仍然看起来不平衡。看来我必须重新计算跨度的中心点。对于非标准尺寸地图,任何人都有解决方案吗?我在foursquare应用程序中注意到它确实有我面临的同样问题,因为当地图很小时,上边缘显示的图钉不合适。

提前致谢

 (void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated
 {

NSArray *annotations = mapView.annotations;
int count = [mapView.annotations count];
if ( count == 0) { return; } //bail if no annotations

//convert NSArray of id <MKAnnotation> into an MKCoordinateRegion that can be used to set the map size
//can't use NSArray with MKMapPoint because MKMapPoint is not an id
MKMapPoint points[count]; //C array of MKMapPoint struct

for( int i=0; i<count; i++ ) //load points C array by converting coordinates to points
{
    CLLocationCoordinate2D coordinate = [(id <MKAnnotation>)[annotations objectAtIndex:i] coordinate];
    points[i] = MKMapPointForCoordinate(coordinate);
}



//create MKMapRect from array of MKMapPoint

MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:count] boundingMapRect];
//convert MKCoordinateRegion from MKMapRect
MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);

//add padding so pins aren't scrunched on the edges
   region.span.latitudeDelta  *= 1.5*ANNOTATION_REGION_PAD_FACTOR;
   region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR;
//but padding can't be bigger than the world
   if( region.span.latitudeDelta > MAX_DEGREES_ARC ) { region.span.latitudeDelta  =    MAX_DEGREES_ARC; }
if( region.span.longitudeDelta > MAX_DEGREES_ARC ){ region.span.longitudeDelta = MAX_DEGREES_ARC; }

  //and don't zoom in stupid-close on small samples
if( region.span.latitudeDelta  < MINIMUM_ZOOM_ARC ) { region.span.latitudeDelta  = MINIMUM_ZOOM_ARC; }
if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; }
//and if there is a sample of 1 we want the max zoom-in instead of max zoom-out
if( count == 1 )
{
    region.span.latitudeDelta = MINIMUM_ZOOM_ARC;
    region.span.longitudeDelta = MINIMUM_ZOOM_ARC;
}
[mapView setRegion:region animated:animated];

}

4

1 回答 1

5

看一下这个,

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

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

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

    for (id <MKAnnotation> annotation in theMapView.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;

    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

    region = [theMapView regionThatFits:region];
    [theMapView setRegion:region animated:YES];
}
于 2013-06-27T10:35:03.610 回答