开发者

Getting position of annotation callout in iOS

开发者 https://www.devze.com 2023-02-14 08:53 出处:网络
here\'s the situation: I am using MapKit to display a number of locations on a map. I have code to calculate the bounds that allow all the points on the map to fit the window. I just recently added co

here's the situation: I am using MapKit to display a number of locations on a map. I have code to calculate the bounds that allow all the points on the map to fit the window. I just recently added code to allow the closest location's callout bubble (proper term?) to appear. Unfortunately, the callout bubble does not fit in the bounds of the window. Ideally, I'd like the zoom to adjust to fit all the annotations as well as the closest location's callout bubble. Any suggestions on how to do this? Here is my zoom method:

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

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

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

    for(WaybackAnnotation *annotation in [mv annotations])
    {
        if ([annotation isKindOfClass:[WaybackAnnotation class]])
        {
            topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
            topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

            bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annota开发者_C百科tion.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; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

    region = [mv regionThatFits:region];
    [mv setRegion:region animated:YES];
}

Thanks in advance! James


Since the bubble is always displayed above the pin, you need some extra padding at the top. You know the height of the padding in screen pixels. Use MKMapViews conversion methods (-convertRect:toRegionFromView: and friends) to convert these into geo coordinates.

0

精彩评论

暂无评论...
验证码 换一张
取 消