【问题标题】:How to zoom MapView annotations below an overlay view?如何在覆盖视图下方缩放 MapView 注释?
【发布时间】:2012-12-20 12:37:14
【问题描述】:

我需要缩放覆盖视图下方的地图注释。

我正在使用this method 放大到 2 个注释。

我似乎无法找到适合叠加视图下方的 2 个注释的方法,因为:

  • 设置地图 edgePadding 会隐藏底部图钉
  • 设置地图中心会隐藏底部图钉
  • 我猜设置 latitudeDelta 会起作用,但如果引脚水平平行,则没有必要 + 它会因不同情况而变得混乱。

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

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

【问题讨论】:

    标签: iphone cocoa android-mapview mapkit mkannotation


    【解决方案1】:

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

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

    希望你会喜欢,干杯

    【讨论】:

    • 这很好,但它仍然不适合覆盖视图下方的注释。它的结果与我当前的代码相同。
    • 透明的黑色覆盖视图只是放在 IB 中的地图视图上
    【解决方案2】:

    如果您想在叠加层上使用触摸点,请为叠加层设置禁用用户交互。

    为了设置缩放级别,我在绘制点后做了一些调整。您可以计算经纬度的平均值,然后根据叠加层的大小调整经度经度。这是我所做的:

                float latAvg,longAvg;
    
    
                latAvg = (location1.latitude + location.latitude)/2;
                longAvg = (location1.longitude + location.longitude)/2;
                MKCoordinateSpan span;// = MKCoordinateSpanMake(0.006, 0.006);
    
                span.latitudeDelta = fabs(location1.latitude - location.latitude);
                span.longitudeDelta = fabs(location1.longitude - location.longitude);
    
    
    //Then adjust the lat longs delta according to your need.
    
                span.latitudeDelta = span.latitudeDelta + 0.0010;
                span.longitudeDelta = span.longitudeDelta + 0.0010;
    
                CLLocationCoordinate2D locationLatlng;
                locationLatlng.latitude = latAvg;
                locationLatlng.longitude = longAvg;
    
                MKCoordinateRegion viewRegion = MKCoordinateRegionMake(locationLatlng, span);
    
    
                [map setRegion:viewRegion animated:YES];
    
                [map regionThatFits:viewRegion];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2013-10-17
      • 2021-11-02
      相关资源
      最近更新 更多