Wednesday, 11 July 2018

Can I view a Google Maps map by bounding latitude and longitude coordinates


With Google Maps I can enter a single lat/lng coordinate and the map is drawn with that lat/lng in the center.


What I'd like to be able to do is enter two coordinate pairs, say the southwest coordinate and the northeast coordinate, and have a Google Map displayed with the appropriate zoom so that the map fills the bounds defined by those coordinates.


Is this possible? If so, how?



Thanks


And the Answer is... Go to Google Maps Code Playground!


Per Taylor's suggestion, I went to the Google Maps Code Playground and entered the following code and was able to get a centered map with the appropriate zoom level bounded by the coordinate pair.


function initialize() {
var mapDiv = document.getElementById('map-canvas');
var map = new google.maps.Map(mapDiv, {
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var southWest = new google.maps.LatLng(..., ...);

var northEast = new google.maps.LatLng(..., ...);
var bounds = new google.maps.LatLngBounds(southWest,northEast);
map.fitBounds(bounds);
}



Answer



The function you are looking for is called fitBounds. The fitBounds function takes a LatLngBounds as its parameter. You can read more about that here.


Example code:


var southWest = new google.maps.LatLng(36.90731625763393,-86.51778523864743);

var northEast = new google.maps.LatLng(37.02763411292923,-86.37183015289304);
var bounds = new google.maps.LatLngBounds(southWest,northEast);
myMap.fitBounds(bounds);

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...