缩放以适合非标准尺寸地图的所有注释

Posted

技术标签:

【中文标题】缩放以适合非标准尺寸地图的所有注释【英文标题】:Zoom to fit all annotation for non-stand size map 【发布时间】:2013-06-27 10:27:07 【问题描述】:

我找到了有关缩放以适应标准尺寸方法的所有注释的主题。我使用了该代码,但问题是地图只有 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];

【问题讨论】:

【参考方案1】:

看看这个,

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

【讨论】:

感谢您的回复。但还是不行。我认为问题是我的地图有宽度>高度但标准地图有高度>宽度 我们可以使用上面的代码设置最小缩放级别吗?那么请指导我,

以上是关于缩放以适合非标准尺寸地图的所有注释的主要内容,如果未能解决你的问题,请参考以下文章

缩放以适合 LeafletJS 地图上的所有标记

适合放大 iPhone 上的地图

在 GMSMapView 上确定适当的缩放级别以适合所有需要的位置?

如何将 Bing 地图的缩放级别设置为足够宽以显示所有图钉?

缩放 d3 v4 地图以适合 SVG(或根本不适合)

缩放 d3 v4 地图以适合 SVG(或根本不适合)