iphone - 如何缩放覆盖 View 下方的 MapView 注释?

标签 iphone cocoa android-mapview mapkit mkannotation

我需要缩放重叠 View 下方的 map 注释。

我正在使用this method缩放至 2 个注释。

我似乎找不到一种方法来适应覆盖 View 下方的 2 个注释,因为:

  • 设置 map edgePadding将隐藏底部图钉
  • 设置 map 中心将隐藏底部图钉
  • 我想设置 latitudeDelta 会起作用,但如果引脚水平平行,则没有必要 + 在不同的情况下会变得困惑。

所以我想知道是否有一个优雅的解决方案可以解决我可能错过的这个问题?

The problem

此覆盖层还隐藏了注释标注,我想如果我有第一个问题的解决方案,这可以解决。

最佳答案

也许您应该尝试使用此代码来处理注释的完美配合:

- (void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated
{ 
    NSArray *annotations = mapView.annotations;
    int count = [mapView.annotations count];
    if ( count == 0) { return; } //return 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  *= 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];
}

因此,您必须定义填充、最大度数弧和最小缩放弧。对于前。应该是这样的:

#define MINIMUM_ZOOM_ARC 0.05 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.25
#define MAX_DEGREES_ARC 360

希望你会喜欢它,干杯

关于iphone - 如何缩放覆盖 View 下方的 MapView 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13972391/

相关文章:

cocoa - 如何解释触控板捏合手势来缩放 IKImageBrowserView

iphone - MapView - 让注释一次出现一个

iPhone 多线程和 AI

NSTableView 单元格中的 cocoa NSView

cocoa - 如何以编程方式从 WebView 对象中弹出 Webkit 的 Web Inspector?

Android MapView MapController stopAnimation 不停止动画

android - Nutiteq MapView 对长按没有反应

iphone - iPhone SDK 中的透明按钮

ios - Sprite Kit 碰撞检测

ios - 您的二进制文件未针对 iPhone 5 进行优化,LaunchImage {320,568}