tags:

views:

213

answers:

2

I have a map that shows location points based on the gbounds of the map. For example, any time the map is moved/zoomed, i find the bounds and query for locations that fall within those bounds. Unfortunately I'm unable to display all my locations when fully zoomed out. Reason being, gmaps reports the min/max long as whatever is at the edge of the map, but if you zoom out enough, you can get a longitudinal range that excludes visible locations.

For instance, if you zoom your map so that you see NorthAmerica twice, on the far left and far right. The min/max long are around: -36.5625 to 170.15625. But this almost completely excludes NorthAmerica which lies in the -180 to -60 range. Obviously this is bothersome as you can actually see the continent NorthAmerica (twice), but when I query my for locations in the range from google maps, NorthAmerica isn't returned.

My code for finding the min/max long is:

bounds = gmap.getBounds();
min_lat = bounds.getSouthWest().lat()
max_lat = bounds.getNorthEast().lat()

Has anyone encountered this and can anyone suggest a workaround? Off the top of my head I can only thing of a hack: to check the zoom level and hardcode the min/max lats to -180/180 if necessary, which is definitely unacceptable.

A: 

This isn't the most elegant solution, but I tested it and it does work. Check to see if the current bounds contains the bounds of the full map. Google doesn't actually include the north and south pole, so I had to tweak it to go almost all the way north and south.

bounds = gmap.getBounds();
var fullEarth = new GLatLngBounds(new GLatLng(-85, 180), new GLatLng(85, -180)))
var isFull = bounds.containsBounds(fullEarth);
min_lat = isFull ? -90 : bounds.getSouthWest().lat()
max_lat = isFull ? 90 : bounds.getNorthEast().lat()

min_lng = isFull ? 180 : bounds.getSouthWest().lng()
max_lng = isFull ? -180 : bounds.getNorthEast().lng()
Mark
It's a shame that the isFullLng() doesn't work, this was my first thought also! Thanks for the fullEarth code, as you say it's ugly, but it works. BTW, your min_lng should be negative, you've got it backwards!
brad
p.s. i think that's a serious oversight on google's part. the isFullLat() is obviously using the getNorthEast functions for testing against max vals. This leads to unexpected results
brad
update: sorry, I had to unaccept this there's still a slight bug. You can move the latitude around so that the min/max is, say, -60/88, in which case the containsBounds returns false, so I'm back to the odd longitude problem and I can still see pretty much the full map
brad
this is getting even more weird, when you zoom out, so that hte map overlaps a bit, sometimes bounds.getSouthWest().lng() returns the lng at the SouthEast Corner. Try it! Zoom out so you can see NorthAmerica twice and put alaska at the left edge of the map. You'd expect bounds.getSouthWest().lng() to be about -165, but instead it's -14, which is the East Edge Lng!! I'm very confused
brad
A: 

Can not even imagine how this HUGE bug is still there !!! isFullLng and toSpan functions are buggy and you can not rely on them.
My bug workaround for this:

var zoom = map.getZoom();
var bounds = map.getBounds();    
var span = bounds.toSpan();
var isFull = span.lng() > 277 || zoom < 2;
kolimarfey