How to get real borders with Google maps when they are completely reduced

I have a map that shows location points based on a gbounds map. For example, at any time when the map moves / scales, I find boundaries and queries for locations that fall within these boundaries. Unfortunately, I was not able to display all my locations when I completely scaled down. The reason is that gmaps reports that min / max is longer than everything that is on the edge of the map, but if you zoom out enough, you can get a longitudinal range that excludes visible places.

For example, if you zoom in on your map to see NorthAmerica twice, in the far left and far right. The minimum / maximum length is from -36.5625 to 170.15625. But that almost completely excludes NorthAmerica, which ranges from -180 to -60. Obviously, this is annoying, since you can see the NorthAmerica continent (twice), but when I ask for location data from Google maps from me, NorthAmerica does not return.

My code to search for min / max long:

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

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

+6
google-maps
source share
3 answers

This is not the most elegant solution, but I tested it and it works. Make sure the current borders contain the borders of the full map. Google doesn’t actually include the north and south poles, so I had to adjust 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() 
0
source share

I can’t even imagine how this HUGE mistake is still there !!! the isFullLng and toSpan functions do not work, and you cannot rely on them.
My mistake for this is:

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

Here is my solution, I only need isFullLng (), which, apparently, has been removed from the v3 API:

 isFullLng: function() { var scale = Math.pow(2, map.getZoom()), bounds = map.getBounds(), ne = bounds.getNorthEast(), sw = bounds.getSouthWest(), lat = (ne.lat() <= 0 && sw.lat() >= 0) || (ne.lat() >= 0 && sw.lat() <= 0) ? 0 : Math.min(Math.abs(ne.lat()), Math.abs(sw.lat())), // closest latitude to equator deg1 = new google.maps.LatLng(lat, 0), deg2 = new google.maps.LatLng(lat, 1), coord1 = map.getProjection().fromLatLngToPoint(deg1), coord2 = map.getProjection().fromLatLngToPoint(deg2); // distance for one long degree in pixels for this zoom level var pixelsPerLonDegree = (coord2.x - coord1.x) * scale; // width of map holder should be <= 360 (deg) * pixelsPerLonDegree if full map is displayed var width = $this.width(); // width of map holder div return pixelsPerLonDegree * 360 <= width; }, isFullLat: function() { var bounds = map.getBounds(), ne = bounds.getNorthEast(), sw = bounds.getSouthWest(), maxLat = 85; // max lat degree return ne.lat() >= maxLat && sw.lat() <= -maxLat; }, 

So, isFull could be:

isFullLng () && isFullLat ()

Note that in isFullLng () the width of the map placeholder is required, I use jQuery and the map is displayed in the div referenced by $ this, so I call

$ this.width ()

You should change this to apply it to your problem.

0
source share

All Articles