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.

[MGLMapView zoomToSouthWestCoordinate:northEastCoordinate:] not coordinates deterministic #1433

Closed

Description

@friedbunny

Using [MGMapView zoomToSouthWestCoordinate:northEastCoordinate:animated:] produces a different result based on the zoom/scale of the map.

I expected the method to always fit the given coordinates on the screen — instead, it zooms in on their center point at a zoom level calculated based on _mbglMap->getScale(). This often results in z18 and never actually fits the viewport to the exact coordinate bounds.

Calling the method repeatedly with the same coordinates but at different zooms yields this:

Mapbox GL: input-zoom: 13.45   scale: 11161.71     output-zoom: 18.00
Mapbox GL: input-zoom: 15.78   scale: 56414.93     output-zoom: 18.00
Mapbox GL: input-zoom: 3.69    scale: 12.91        output-zoom: 9.18
Mapbox GL: input-zoom: 9.18    scale: 578.48       output-zoom: 14.66
Mapbox GL: input-zoom: 14.66   scale: 25918.91     output-zoom: 18.00

[MGMapView zoomToSouthWestCoordinate:northEastCoordinate:animated:] is only used right now for centering the map on the user location annotation and is somewhat broken for that purpose, too, besides not being a public method. (#1092 tracks what to do about this API decision.)

I would like to use this method for bounds-based syncing in the demo app I'm working on, so I've hacked together an interim solution, but the maths are absolutely wrong:

- (void)fitBoundsToSouthWestCoordinate:(CLLocationCoordinate2D)southWestCoordinate northEastCoordinate:(CLLocationCoordinate2D)northEastCoordinate animated:(BOOL)animated
{
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((northEastCoordinate.latitude + southWestCoordinate.latitude) / 2, (northEastCoordinate.longitude + southWestCoordinate.longitude) / 2);

    CGFloat scaleX = _mbglMap->getWidth() / (northEastCoordinate.longitude - southWestCoordinate.longitude);
    CGFloat scaleY = _mbglMap->getHeight() / (northEastCoordinate.latitude - southWestCoordinate.latitude);
    CGFloat minZoom = _mbglMap->getMinZoom();
    CGFloat maxZoom = _mbglMap->getMaxZoom();
    CGFloat hack = ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) ? 0.5f : 1.f;
    CGFloat zoomLevel = log(MIN(scaleX, scaleY)) / log(2) - hack;
    zoomLevel = MAX(MIN(zoomLevel, maxZoom), minZoom);

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

I put together a demonstration of this bug and my hack in a branch here. Use the action sheet's "Zoom to Oregon" option to see new default behavior.

/cc @incanus @1ec5

Activity

  1. friedbunny commented on May 6, 2015

    @friedbunny
    ContributorAuthor

    My hack inexplicably appears to work very well, I can make it match Google's fitBounds perfectly (although without UIEdgeInsets support). This will be sufficient for Treble, but clearly our bounds fitting code needs cleaning up and somebody to explain/correct the math.

  2. changed the title [-][MGMapView zoomToSouthWestCoordinate:northEastCoordinate:] not coordinates deterministic[/-] [+][MGLMapView zoomToSouthWestCoordinate:northEastCoordinate:] not coordinates deterministic[/+] on May 24, 2015
  3. self-assigned this
    on Jun 23, 2015
  4. added this to the iOS Beta 3 milestone on Jun 23, 2015
  5. added
    iOS
    on Jun 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugiOS

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions