Centering a Google Map on America

Earlier this morning I was building a Google Map demo for a client (using EventBrite data – I’ll share that if I can) and I needed to center a Google Map on America. There are a couple ways of doing this and I thought I’d share them along with some screen shots so you can see the results.

The first Google result I found led to this Stack Overflow result: How to center Google Map on a country by name. The answer uses Geocoding to translate a simple country name, like America, into the right results. You’ll need to scroll down a bit to see a version of the code for the current version of Google’s API, or just check out the full sample below.

<!DOCTYPE html>
<html>

<head>
<title>Demo One</title>
<style type="text/css">
#map_canvas { width: 500px; height: 500px; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {

	var country = "United States"

    var myOptions = {
        zoom: 3,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
	
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);

	var geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': country }, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
		} else {
			alert("Could not find location: " + location);
		}
	});

}
</script>
</head>

<body onload="initialize()">

<div id="map_canvas"></div>

</body>
</html>

This gives you:

Looks good to me – but the geocoding bugs me. For every single visitor to your site, a request will be made to Google to ask it where America is. Most likely, America is not going to move. Probably. And while this request is pretty darn fast, there’s no real reason for you to geocode this constantly. I did another quick Google and discovered this Wikipedia page: Geographic center of the contiguous United States. It defined the longitude and latitude for America as 39°50?N 98°35?W. I rewrote my code to simply use these hard coded values.

<!DOCTYPE html>
<html>

<head>
<title>Demo Two</title>
<style type="text/css">
#map_canvas { width: 500px; height: 500px; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {

    var latlng = new google.maps.LatLng(39.5, -98.35);
    var myOptions = {
        zoom: 3,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

	var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);

}
</script>
</head>

<body onload="initialize()">

<div id="map_canvas"></div>

</body>
</html>

And the result:

Looks a tiny bit different to me. So I went back to the first demo and added this line: console.log(results[0].geometry.location.lat(), results[0].geometry.location.lng());. I checked the console for the values and simply updated the numbers.

I zipped up the demos (including a third demo with the values returned via the console) and included it as a zip.

Download attached file.