Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was 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

Description

@tomtaylor

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!)

Activity

  1. 1ec5 commented on Jun 23, 2015

    @1ec5
    Contributor

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

  2. tomtaylor commented on Jun 23, 2015

    @tomtaylor
    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.

  3. picciano commented on Jun 23, 2015

    @picciano

    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];
    }
    
  4. tomtaylor commented on Jun 23, 2015

    @tomtaylor
    Author

    @picciano amazing, thanks!

  5. self-assigned this
    on Jun 23, 2015
  6. tomtaylor commented on Jun 26, 2015

    @tomtaylor
    Author

    Great, thanks!

    On 26 Jun 2015, at 06:10, Minh Nguyễn [email protected] wrote:

    Closed #1775 via #1783.

    —
    Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions