Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Centering a Google Map on America

DZone's Guide to

Centering a Google Map on America

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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



Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}