Latitude and Longitude of a location using Javascript and Google

The following Javascript will help you acquire the latitude and longitude of a location (using an address, zip code, or city).

Note: You will need to have a Google API key which is domain specific.

// Place the following code in the <head> section
<script type="text/javascript">
 
var geocoder;
geocoder = new GClientGeocoder();
 
function submitForm() {
  var address = document.getElementById("address").value + ", " + document.getElementById("city").value + ", " + document.getElementById("state").value + ", " + document.getElementById("zip").value;
  geocoder.getLocations(address, getCoords);
}
 
function getCoords(response) {
  if (!response || response.Status.code != 200) {
	alert("Unable to Create Lat and Lng for plotting.");
  } else {
	place = response.Placemark[0];
	var lat = place.Point.coordinates[1];
	var lng = place.Point.coordinates[0];
  }
 alert("Lat:"+lat+" - Lng:"+lng);
}
</script>

Here is the HTML that was used to pass the data:

Address: <input id="address" name="address" type="text" />
 
City: <input id="city" name="city" type="text" />
 
State: <input id="state" name="state" type="text" />
 
Zip: <input id="zip" name="zip" type="text" />
 
<input onclick="submitForm();" type="button" value="Submit" />