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

map getBounds works incorrectly on a date line cross #295

Closed
jfgirard opened this issue Sep 15, 2011 · 12 comments
Closed

map getBounds works incorrectly on a date line cross #295

jfgirard opened this issue Sep 15, 2011 · 12 comments
Assignees
Labels
Milestone

Comments

@jfgirard
Copy link
Contributor

Hi,
I have an issue using the getBounds function of the map object. If the map extent is out of the typical longitude range [-180, 180], the getBounds returns what is NOT visible. For example, if the map shows from Japan to New-York, the returned bounds are: _southWest: lat: -17.9, lng: -67.5
_northEast: lat: 79.8, lng: 95.6

This is what is NOT visible (south america to north russia). Indeed, the map center is not contained in the bounds.
The getBounds method could return an array of bounds when is out of the [-180,180] range. From X to 180 and -180 to Y.

What do you think ?

Jeff

@weitzj
Copy link

weitzj commented Sep 15, 2011

Hi,

I have the same problem. To resolve it I test manually if the mapcenter is inside the bounds. This fails and then I know I got the bounds for the wrong side of the earth.

@ghost ghost assigned mourner Sep 23, 2011
@mourner
Copy link
Member

mourner commented Sep 23, 2011

Thanks, will think about this.

@dylanmckendry
Copy link

Hi weitzj is it possible to have a look at your code to fix this problem? I can see how your solution would work when the world is zoomed out enough and I have that working. But if we are zoomed in more the it doesnt work...

@zedd45
Copy link
Contributor

zedd45 commented Dec 9, 2011

I tried this, personally.

   /**
     * temp fix to address https://github.com/CloudMade/Leaflet/issues/295
     * @return L.LatLngBounds - mapBounds - the unaltered mapBounds object, if the bug is not triggered, else return the entire earth 
     */
    getSafeMapBounds: function(mapBounds){
        var mapBounds = this._map.getBounds();
        if (!mapBounds.contains(this._map.getCenter())) {
            return new L.LatLngBounds(new L.LatLng(-60, -170), new L.LatLng(85, 179)); //The World!
        }
        return mapBounds;
    },

That said, I haven't had a chance to test it extensively, yet, but it appears to work for my purposes (I can use the entire world instead of just the bounds, in my case)

@weitzj
Copy link

weitzj commented Dec 9, 2011

Well, this is my fix: you need to switch the longitudes, since they get flipped:

/**
* Fetches all maps in the given bounding box.
* @param {L.Bounds} bounds BoundingBox.
* @param {L.LatLng} center Center of BoundingBox.
*/
function getAndDrawNewMapData(bounds, center) {

    var swLat = bounds.getSouthWest().lat;
    var swLon = bounds.getSouthWest().lng;
    var neLat = bounds.getNorthEast().lat;
    var neLon = bounds.getNorthEast().lng;

    //flip, if LeafLet messes up <==> center is no longer in current bounds
    if(!bounds.contains(center)) {
        var tmpLon = neLon;
        neLon = swLon;
        swLon = tmpLon;
    }
    .........

@mourner
Copy link
Member

mourner commented Dec 10, 2011

Thanks for sharing guys, I'll try to incorporate a similar fix to the upcoming stable release.

@zedd45
Copy link
Contributor

zedd45 commented Dec 12, 2011

Thanks Weitzj. I'll update mine. That seems to be a more intelligent solution, now that I better understand the problem!
Thanks Mourner. This is why I support this lib. You're doing a fantastic job. Keep up the good work.

@mourner
Copy link
Member

mourner commented Dec 16, 2011

Should be properly fixed now, guys, check it out!

@jfgirard
Copy link
Contributor Author

Good job, it works well.

@zedd45
Copy link
Contributor

zedd45 commented Dec 16, 2011

Works great! Thanks, Mourner!

@mourner
Copy link
Member

mourner commented Dec 16, 2011

Awesome, thanks :)

@dylanmckendry
Copy link

Super thanks Mourner! Works a treat!

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

No branches or pull requests

5 participants