Updated! Make a Google Map That Directs People To Headquarters

For this tutorial we are going to make a map that allows users to type in a location into an input field and the map will show Google’s recommended driving route to our headquarters.

This would be particularly useful for helping customers find a store, event, or other brick and mortar location.

Embedding a Google Map onto your website can accomplish the same thing, but with this example, we can have the following customizations:

  • Custom Headquarters Marker
  • Custom Info Window
  • Custom Map Style

We’re going to start off with a basic HTML5 template.

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
  	<title>Updated! How To Make A Google Map That Gives Directions</title>
</head>
<body>
	
</body>
</html>

The next thing we’re going to add is our Google Maps API key. This can be retrieved from the Google Maps API Signup page. Keep in mind we’re using v.3, which has been enhanced for mobile and tablet devices. I’ve had experience working with v.2 and trying to make it work properly on mobile devices is an uphill battle, so don’t use it.

Add this somewhere before the </head> tag.

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22http%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fjs%3Fkey%3D%5Binsert-your-API-key-here%5D%26amp%3Bsensor%3Dfalse%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

 

In the <body> section add our map canvas.


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

 

 

Now we need to add some javascript to get this map in there. This can be added somewhere above the closing </body> tag.

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%09%09var%20directionDisplay%3B%0A%09%09var%20directionsService%20%3D%20new%20google.maps.DirectionsService()%3B%0A%09%09var%20map%3B%0A%0A%09%09function%20initialize()%20%7B%0A%09%09%09directionsDisplay%20%3D%20new%20google.maps.DirectionsRenderer()%3B%0A%09%09%09var%20headquarters%20%3D%20new%20google.maps.LatLng(51.0426981%2C-114.0737685)%3B%09%0A%09%09%09var%20myOptions%20%3D%20%7B%0A%09%09%09%09zoom%3A13%2C%0A%09%09%09%09mapTypeId%3A%20google.maps.MapTypeId.ROADMAP%2C%0A%09%09%09%09center%3A%20headquarters%0A%09%09%09%7D%0A%09%09%09map%20%3D%20new%20google.maps.Map(document.getElementById(%22map_canvas%22)%2C%20myOptions)%3B%0A%09%09%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Add%20A%20Marker%20For%20Our%20Location%0A%09%09%09var%20headquartersMarker%20%3D%20new%20google.maps.Marker(%7B%0A%09%09%09%09position%3A%20headquarters%2C%0A%09%09%09%09map%3A%20map%2C%0A%09%09%09%09animation%3A%20google.maps.Animation.DROP%2C%20%2F%2F%20not%20necessary%2C%20but%20looks%20nice%20%0A%09%09%09%09zIndex%3A99999%0A%09%09%09%7D)%3B%0A%0A%09%09%09directionsDisplay.setMap(map)%3B%0A%09%09%7D%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

 

To make this load, we need to add the following to our <body> tag so it looks like this:

<body onload="initialize()">

 

With what we’ve created so far, you should see a Google Map. Now we need to add the form and the javascript that allows a user to type in a their address, postal code, or city.

Below the initialize() function inside the <head> tags, add the following:

// Calculate the route  
		function calcRoute() {
			var start = document.getElementById("start").value;
			var end = new google.maps.LatLng(51.0426981,-114.0737685);
			var request = {
				origin:start, 
				destination:end,
				travelMode: google.maps.DirectionsTravelMode.DRIVING
			};
		 
			directionsService.route(request, function(response, status) {
				if (status == google.maps.DirectionsStatus.OK) {
					directionsDisplay.setDirections(response);
				}
			});
		 
			var endMarker = new google.maps.Marker({
				position: end,
				map: map,
			});
		} 

 

For the form and to add some style to our demo, we will add this below the <body> tag, but above our map-canvas.

<div id="content">
		
<h1>Get Me Driving Directions</h1>

		
		
<div>
			<input type="text" name="start" id="start" value="Enter Address, Postal Code, City, etc." onclick="document.getElementById(this.id).value= '';" />
			<button onclick="calcRoute()">Submit</button>
			<button onclick="initialize()">Reset</button>
		</div>

</div>

<!-- #content -->	

 

We now have an input box and 2 buttons. Submit will calculate the route to our headquarters and reset will set the map to it original status. In the input box, I have left a small script to reset the value upon clicking it. It makes things much easier for the user and looks professional.

For presentation’s sake, here is some simple CSS  to add above the javascript.

<link href='http://fonts.googleapis.com/css?family=Redressed' rel='stylesheet' type='text/css'>
  
  	<!-- CSS -->
	
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%09%09*%20%7B%20%0A%09%09%09margin%3A%200%3B%20%0A%09%09%09padding%3A%200%3B%20%0A%09%09%7D%0A%09%09body%20%7B%0A%09%09%09padding%3A20px%3B%0A%09%09%09font-family%3A%20helvetica%2C%20arial%2C%20sans-serif%3B%0A%09%09%7D%0A%09%09h1%20%7B%0A%09%09%09color%3A%23fff%3B%0A%09%09%09font-family%3A%20'Redressed'%2C%20cursive%3B%0A%09%09%09line-height%3A%20130%25%3B%0A%09%09%7D%0A%09%09%23content%20%7B%0A%09%09%09padding%3A20px%200%2020px%2020px%3B%0A%09%09%09background%3A%20%23000%3B%0A%09%09%7D%0A%09%09%23map_canvas%20%7B%0A%09%09%09width%3A%20100%25%3B%0A%09%09%09height%3A%20500px%3B%0A%09%09%7D%0A%09%09%23start%20%7B%0A%09%09%09padding%3A%206px%3B%0A%09%09%09width%3A%20260px%3B%0A%09%09%09color%3A%20%23000%3B%0A%09%09%7D%0A%09%09button%20%7B%0A%09%09%09width%3A%2080px%3B%0A%09%09%09height%3A%2029px%3B%0A%09%09%09border%3A%20none%3B%0A%09%09%09color%3A%20%23fff%3B%0A%09%09%09cursor%3A%20pointer%3B%0A%09%09%09background%3A%20%23ccc%3B%0A%09%09%7D%0A%09%09button%3Ahover%20%7B%0A%09%09%09color%3A%20%23000%3B%0A%09%09%09background%3A%20%23eee%3B%0A%09%09%7D%0A%20%20%09%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />

 

Here is what the complete code should look like:

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
  	<title>How To Make A Google Map That Gives Directions</title>
  	<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22http%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fjs%3Fkey%3D%5Binsert-your-API-key-here%5D%26amp%3Bsensor%3Dfalse%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
  	
  	<link href='http://fonts.googleapis.com/css?family=Redressed' rel='stylesheet' type='text/css'>
  
  	<!-- CSS -->
	
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%09%09*%20%7B%20%0A%09%09%09margin%3A%200%3B%20%0A%09%09%09padding%3A%200%3B%20%0A%09%09%7D%0A%09%09body%20%7B%0A%09%09%09padding%3A20px%3B%0A%09%09%09font-family%3A%20helvetica%2C%20arial%2C%20sans-serif%3B%0A%09%09%7D%0A%09%09h1%20%7B%0A%09%09%09color%3A%23fff%3B%0A%09%09%09font-family%3A%20'Redressed'%2C%20cursive%3B%0A%09%09%09line-height%3A%20130%25%3B%0A%09%09%7D%0A%09%09%23content%20%7B%0A%09%09%09padding%3A20px%200%2020px%2020px%3B%0A%09%09%09background%3A%20%23000%3B%0A%09%09%7D%0A%09%09%23map_canvas%20%7B%0A%09%09%09width%3A%20100%25%3B%0A%09%09%09height%3A%20500px%3B%0A%09%09%7D%0A%09%09%23start%20%7B%0A%09%09%09padding%3A%206px%3B%0A%09%09%09width%3A%20260px%3B%0A%09%09%09color%3A%20%23000%3B%0A%09%09%7D%0A%09%09button%20%7B%0A%09%09%09width%3A%2080px%3B%0A%09%09%09height%3A%2029px%3B%0A%09%09%09border%3A%20none%3B%0A%09%09%09color%3A%20%23fff%3B%0A%09%09%09cursor%3A%20pointer%3B%0A%09%09%09background%3A%20%23ccc%3B%0A%09%09%7D%0A%09%09button%3Ahover%20%7B%0A%09%09%09color%3A%20%23000%3B%0A%09%09%09background%3A%20%23eee%3B%0A%09%09%7D%0A%20%20%09%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />

  
  	<!-- Javascript -->
  	<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%09%09var%20directionDisplay%3B%0A%09%09var%20directionsService%20%3D%20new%20google.maps.DirectionsService()%3B%0A%09%09var%20map%3B%0A%0A%09%09function%20initialize()%20%7B%0A%09%09%09directionsDisplay%20%3D%20new%20google.maps.DirectionsRenderer()%3B%0A%09%09%09var%20headquarters%20%3D%20new%20google.maps.LatLng(51.0426981%2C-114.0737685)%3B%09%0A%09%09%09var%20myOptions%20%3D%20%7B%0A%09%09%09%09zoom%3A13%2C%0A%09%09%09%09mapTypeId%3A%20google.maps.MapTypeId.ROADMAP%2C%0A%09%09%09%09center%3A%20headquarters%0A%09%09%09%7D%0A%09%09%09map%20%3D%20new%20google.maps.Map(document.getElementById(%22map_canvas%22)%2C%20myOptions)%3B%0A%09%09%20%0A%09%09%09%2F%2F%20Add%20A%20Marker%20For%20Our%20Headquarters%20%0A%09%09%09var%20headquartersMarker%20%3D%20new%20google.maps.Marker(%7B%0A%09%09%09%09position%3A%20headquarters%2C%0A%09%09%09%09map%3A%20map%2C%0A%09%09%09%09animation%3A%20google.maps.Animation.DROP%2C%0A%09%09%09%09zIndex%3A99999%0A%09%09%09%7D)%3B%0A%09%09%20%0A%09%09%09directionsDisplay.setMap(map)%3B%0A%09%09%7D%0A%0A%09%09%2F%2F%20Calculate%20the%20route%20%20%0A%09%09function%20calcRoute()%20%7B%0A%09%09%09var%20start%20%3D%20document.getElementById(%22start%22).value%3B%0A%09%09%09var%20end%20%3D%20new%20google.maps.LatLng(51.0426981%2C-114.0737685)%3B%0A%09%09%09var%20request%20%3D%20%7B%0A%09%09%09%09origin%3Astart%2C%20%0A%09%09%09%09destination%3Aend%2C%0A%09%09%09%09travelMode%3A%20google.maps.DirectionsTravelMode.DRIVING%0A%09%09%09%7D%3B%0A%09%09%20%0A%09%09%09directionsService.route(request%2C%20function(response%2C%20status)%20%7B%0A%09%09%09%09if%20(status%20%3D%3D%20google.maps.DirectionsStatus.OK)%20%7B%0A%09%09%09%09%09directionsDisplay.setDirections(response)%3B%0A%09%09%09%09%7D%0A%09%09%09%7D)%3B%0A%09%09%20%0A%09%09%09var%20endMarker%20%3D%20new%20google.maps.Marker(%7B%0A%09%09%09%09position%3A%20end%2C%0A%09%09%09%09map%3A%20map%2C%0A%09%09%09%7D)%3B%0A%09%09%7D%20%0A%09%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />  
</head>
<body onload="initialize()">
	
<div id="content">
		
<h1>Get Me Driving Directions</h1>

		
		
<div>
			<input type="text" name="start" id="start" value="Enter Address, Postal Code, City, etc." onclick="document.getElementById(this.id).value= '';" />
			<button onclick="calcRoute()">Submit</button>
			<button onclick="initialize()">Reset</button>
		</div>

	</div>

<!-- #content -->	
		
	
<div id="map_canvas"></div>

 
</body>
</html>

 

The headquarters used in this demo is Universal Supplements, but you will want to direct people to your own location. To use a location of your choosing, I use Get Lat Lon to retrieve latitude and longitude coordinates.

Summary

We’ve made a map that will visually deliver driving instructions to a specific location by using a few features available in Google Maps v.3. We’ve used the following functions of the API to make this possible:

There are many things that can be built by using this example. The Directions Service can supply directions for cyclists, GeoLocation could be used to make it location aware and create instructions based on where you’re standing, and you can even have it display directions in text format for someone to read to the driver.

Hope you enjoyed this article and happy coding!