Skip to content

Find Latitude and Longitude of a Location: Google Map API

July 1, 2009

Recently I handled a project that required map location (Latitude and Longitude) based on radius from a user defined point. The user enter the location (i.e.) Country, State, City with in a certain miles or kilometers (10, 20, 50, 500…. Any) are found from the database.

This solution uses Google map API. Already we have seen some posts regarding Google map API. This is a another example to find the latitude and longitude according to the location.

Example, While registration of a user i’ll store the latitude and longitude of a user. For that,

Location

Location

From the above location we are going to find the latitude and longitude,


function storeMapValue()
{
   var state = document.getElementById("state").selectedIndex;
   var country = document.getElementById("country").selectedIndex;
   var city = document.getElementById("city").selectedIndex;

   var value = document.getElementById("city").options[city].text+
        ","+document.getElementById("state").options[state].text+
        ","+document.getElementById("country").options[country].text;
   alert(value); // Madurai,Tamil Nadu, India
   var add = new showAddress(value); // Sending the location to
   another function
}
var map = null;
var geocoder = null;

function showAddress(address)
{
   geocoder = new GClientGeocoder();
   geocoder.getLocations(address,function(point)
   {
       if (!point)
       {
          alert(address + " not found");
       }
       else
       {
          place=point.Placemark[0]; // if you alert place
          get [object] [object]
          point=new GLatLng(place.Point.coordinates[1],
          place.Point.coordinates[0]); //Spliting the latitude and
          longitude

          document.getElementById("lat").value =
          place.Point.coordinates[1];
          document.getElementById("lang").value =
          place.Point.coordinates[0];
          //Storing the latitude and longitude in a textbox may
            be hidden.
       }
    });
}

By using the sql query in the previous post we can calculate the distance beetween the two locations. This module is mainly used for searching.

Advertisements

From → Google API

One Comment
  1. Nice code

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: