Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

October 22, 2009

Latitude and Longitude of a location using Javascript and Google

Filed under: JavaScript,The Coding World — admin @ 2:54 pm

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:

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

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" />

Leave a Reply

© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.