Google maps fitBounds () broken or ..?

<!DOCTYPE html> <html> <head> <title>Google Maps JavaScript API v3 Example: Map Simple</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta charset="UTF-8"> <style type="text/css"> #map_canvas { width: 500px; height: 500px; } </style> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script> <script type="text/javascript"> var map; function initialize() { var myOptions = { zoom : 8, center : new google.maps.LatLng(-34.397, 150.644), mapTypeId : google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); var locationA = { latitude : 37.788081 + 0.0049, longitude : -83.001709 + 0.00019 }; var locationB = { latitude : 37.788081, longitude : -83.001709 }; var sw = new google.maps.LatLng(locationA.latitude, locationA.longitude); var ne = new google.maps.LatLng(locationB.latitude, locationB.longitude); var distance = google.maps.geometry.spherical.computeDistanceBetween(sw, ne); console.log("Distance between SW & NE: " + distance); var bounds = new google.maps.LatLngBounds(sw, ne); console.log(bounds); map.fitBounds(bounds); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map_canvas"></div> </body> </html> 

The above returns the following: enter image description here

Got it by accident during dev tests, cleared everything around this β€œerror” and yet, it really seems to be an error.

Can anyone explain this?

Setting locationA.longitude 0.00019 - 0 shows a valid map, so this is the problem.

0
source share
1 answer

Your borders are not valid, the south is larger than the north, and the east is larger than the west. If you switch the pluses to minus, it will boot. I would just create LatLngBounds with no arguments and use bounds.extend .

 var bounds = new google.maps.LatLngBounds(); bounds.extend(sw); bounds.extend(ne); 
+1
source

Source: https://habr.com/ru/post/1205031/


All Articles