简体   繁体   中英

How to zoom MapView annotations below an overlay view?

I need to zoom the map annotations below the over laid view.

Im using this method to zoom to the 2 annotations.

I can't seem to find a way to fit the 2 annotations below the over laid view, because:

  • Setting map edgePadding will hide the bottom pin
  • Setting the map center will hide the bottom pin
  • Setting the latitudeDelta would work I guess but if pins are horizontally parallel it would not be necessary + it would get messy with different cases.

So i'm wondering if there's an elegant solution for this problem that I might have missed?

问题

This overlay also hides the annotation callout, I guess this could be fixed if I have a solution for the first problem.

Maybe you should try this code to handle perfect fit for Annotations :

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

So, you have to define the Padding, Maximum degree arc and Minimum zoom arc. For Ex. should belike this :

#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

Hopefully, You will like it, Cheers

Set userintercation disabled for the overlay if you want touch points on the overlay.

For setting the zoom level I made a few adjustments after plotting the points. You can calculate the average of the lat longs and then adjust the lat longs as per size of the overlay. Here's what I did:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM