Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zooming #21

Open
Miirek opened this issue Nov 8, 2012 · 2 comments
Open

Zooming #21

Miirek opened this issue Nov 8, 2012 · 2 comments

Comments

@Miirek
Copy link

Miirek commented Nov 8, 2012

Hi,

how can I zoom to display all my annotation/whole polygon - I'm missing [mapView regionThatFits] method. Is there any other way to do it?

Thanks for answer.

M.

@Thallius
Copy link

Create a MKMapRect with the minimum and maximum values of your annotations.
After that make a MKCoordinateRegion from this Rect and use setRegion

MKMapRect regionMapRect;
regionMapRect.origin.x=0;
regionMapRect.origin.y=0;
regionMapRect.size.width=0;
regionMapRect.size.height=0;

while()
{
    point=annotation coordinates

    if(regionMapRect.size.width==0)
    {
        // 2000 and 4000 is the zoom I want if only one annotation is visible. Use what you like
        regionMapRect.origin.x=point.x-2000;
        regionMapRect.origin.y=point.y-2000;
        regionMapRect.size.width=4000;
        regionMapRect.size.height=4000;
    }
    else
    {
        if(point.x<regionMapRect.origin.x)
            regionMapRect.origin.x=point.x;
        if(point.y<regionMapRect.origin.y)
            regionMapRect.origin.y=point.y;
        if(point.x>regionMapRect.origin.x+regionMapRect.size.width)
            regionMapRect.size.width=point.x-regionMapRect.origin.x;
        if(point.y>regionMapRect.origin.y+regionMapRect.size.height)
            regionMapRect.size.height=point.y-regionMapRect.origin.y;
    }
}

regionMapRect.size.width *= 1.2;
regionMapRect.size.height *= 1.2;

MKCoordinateRegion coordinateRegion = MKCoordinateRegionForMapRect(regionMapRect);
[mapView setRegion:coordinateRegion animated:NO];

// Setting the center again will have a better result for me

[mapView setCenterCoordinate:CLLocationCoordinate2DMake((minlat+(maxlat-minlat)/2), (minlon+(maxlon-minlon)/2))];

Regards

Claus

@valikb
Copy link

valikb commented Jan 15, 2013

Hi Claus,

I can see you've selected:
[mapView setRegion:coordinateRegion animated:NO]

Have you also noticed that zoom doesn't work when choose "animated:YES"?
Are there any chances to make it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants