Home > Uncategorized > Get your latitude and longitude in Javascript

Get your latitude and longitude in Javascript

Here’s an AJAX request that gets your longitude and latitude in javascript. It’s using XML, not JSON, since there was some problems using eval() for non-US addresses.

var strUrl = “http://maps.googleapis.com/maps/api/geocode/xml?”;

strUrl += “address=bt489ph “;

strUrl += “&sensor=false”;

ajaxRequest = new XMLHttpRequest();

ajaxRequest.onreadystatechange = function() {

if (ajaxRequest.readyState != 4)

{

return;

};

var xmlDoc = ajaxRequest.responseXML;

var latitude = xmlDoc.getElementsByTagName(‘lat’)[0].firstChild.nodeValue;

var longditude = xmlDoc.getElementsByTagName(‘lng’)[0].firstChild.nodeValue;

alert(latitude);

alert(longditude);

};

ajaxRequest.open(“GET”,strUrl,true);

ajaxRequest.send(null);

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: