Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Expose MGLMapView's zoomToSouthWestCoordinate:northEastCoordinate:animated: method in public header #1775

Closed
tomtaylor opened this issue Jun 23, 2015 · 5 comments · Fixed by #1783
Assignees

Comments

@tomtaylor
Copy link

Is there a reason for not exposing MGLMapView's zoomToSouthWestCoordinate:northEastCoordinate:animated: method publicly? It's very useful to have.

(As an aside, being able to also pass a UIEdgeInsets to pad the zoomed area would be amazingly useful!)

@1ec5
Copy link
Contributor

1ec5 commented Jun 23, 2015

Covered by #1092 and #1433. Edge insets would be useful as well.

@tomtaylor
Copy link
Author

Ah, thanks, understood. Do you know when this is planned for? It's my last issue with an RMMapView port that can't be easily worked around for now.

@picciano
Copy link

For what it's worth, here is the workaround I've been using. This one zooms to fit the current annotations, but as part of the process calculates SW and NE bounds. Adjust the globalWidth value to get the "fit" you need.

static float const minZoom = 1.0f;
static float const maxZoom = 16.0f;
static float const globeWidth = 800.0f;

+ (void)zoomToShowAnnotationsWithMapView:(MGLMapView *)mapView animated:(BOOL)animated {
    [[self class] zoomToShowAnnotationsWithMapView:mapView includeUserLocation:YES animated:animated];
}

+ (void)zoomToShowAnnotationsWithMapView:(MGLMapView *)mapView includeUserLocation:(BOOL)includeUserLocation animated:(BOOL)animated {

    if (mapView.annotations.count < 1) {
        return;
    }

    CLLocationCoordinate2D southWestCoordinate, northEastCoordinate;

    if (includeUserLocation) {
        southWestCoordinate = mapView.userLocation.location.coordinate;
        northEastCoordinate = mapView.userLocation.location.coordinate;
    } else {
        southWestCoordinate = ((TGAnnotation *)mapView.annotations[0]).coordinate;
        northEastCoordinate = ((TGAnnotation *)mapView.annotations[0]).coordinate;
    }


    for (TGAnnotation *annotation in mapView.annotations) {
        CLLocationCoordinate2D coordinate = annotation.coordinate;

        if (southWestCoordinate.latitude > coordinate.latitude) {
            southWestCoordinate.latitude = coordinate.latitude;
        }
        if (southWestCoordinate.longitude > coordinate.longitude) {
            southWestCoordinate.longitude = coordinate.longitude;
        }
        if (northEastCoordinate.latitude < coordinate.latitude) {
            northEastCoordinate.latitude = coordinate.latitude;
        }
        if (northEastCoordinate.longitude < coordinate.longitude) {
            northEastCoordinate.longitude = coordinate.longitude;
        }
    }

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((northEastCoordinate.latitude + southWestCoordinate.latitude) / 2, (northEastCoordinate.longitude + southWestCoordinate.longitude) / 2);

    CGFloat pixels = MIN(mapView.bounds.size.height, mapView.bounds.size.width);

    CGFloat angle = MAX(northEastCoordinate.latitude - southWestCoordinate.latitude, northEastCoordinate.longitude - southWestCoordinate.longitude);
    if (angle < 0) {
        angle += 360;
    }
    CGFloat calculatedZoom = log(pixels * 360 / angle / globeWidth) / log(2);
    CGFloat zoomLevel = MAX(MIN(calculatedZoom, maxZoom), minZoom);

    [mapView setCenterCoordinate:center zoomLevel:zoomLevel animated:animated];
}

@tomtaylor
Copy link
Author

@picciano amazing, thanks!

@1ec5 1ec5 self-assigned this Jun 23, 2015
@1ec5 1ec5 mentioned this issue Jun 23, 2015
9 tasks
@1ec5 1ec5 removed the in progress label Jun 26, 2015
@tomtaylor
Copy link
Author

Great, thanks!

On 26 Jun 2015, at 06:10, Minh Nguyễn notifications@github.com wrote:

Closed #1775 via #1783.


Reply to this email directly or view it on GitHub.

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

Successfully merging a pull request may close this issue.

3 participants