Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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);
}

share|improve this question
1  

2 Answers 2

up vote 4 down vote accepted

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);
share|improve this answer
    
Taylor: Is it possible to do this from the maps.google.com website without having to use their API? –  Scott Mitchell Feb 1 '13 at 22:11
    
No, not that I am aware of. What is your use case for this? –  Taylor H. Feb 1 '13 at 23:12
    
I have a variety of coordinate pairs (around 10) and I want to print a map for each coordinate pair. I've used Google Maps API before so I could whip up some code, but I was hoping I could just go to the maps.google.com site, enter my first set of coordinates, hit print, enter the second set, etc. –  Scott Mitchell Feb 1 '13 at 23:15
2  
Try playing around with the Google Code Playground Maps section. You can edit the javascript and html and run it right there. If nothing else it will help you set up your code. –  Taylor H. Feb 2 '13 at 5:03
    
Taylor, the Google Code Playground Maps section was perfect and fit my needs. Thank you for your time and assistance on this matter. –  Scott Mitchell Feb 4 '13 at 16:23

Even simpler is embed the link in an Iframe element in a web page. Take the link at the livel zoom you like, framing your area of interest in a nice way. You could try it out on google sites with a free website, then cut and paste the code into your application.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.