API / Geocoding / Programmings

Geocoding with Google Maps

Geocoding is the process of converting addresses into geographic coordinates which can use to place markers or positions in Map. The places with its cordinates stored in database can be used to  mark locations in any maps.Now google limits the no. of geocoding requests to 2500 queries per day.(User of Google Maps API for Business may perform up to 100,000 requests per day.) This limit is enforced to prevent abuse and repurposing of the Geocoding API .

We can have response in two formats, JSON and XML.

General format of geocoding request is as follows,

http://maps.googleapis.com/maps/api/geocode/output?parameters

Where output can be json or xml as your need.Required parameters are address and sensor where sensor Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be either true or false. https:// is recomended for application includes sensitive data such as user location, in requests.

For example, A geocode request for the address Technopark,Trivandrum,Kerala is like this,

http://maps.googleapis.com/maps/api/geocode/json?address=Technopark,+Trivandrum,+Kerala&sensor=false

Its json response is as follows,

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Technopark",
               "short_name" : "Technopark",
               "types" : [ "bus_station", "transit_station", "establishment" ]
            },
            {
               "long_name" : "Trivandrum Bypass Rd",
               "short_name" : "Trivandrum Bypass Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Technopark Campus",
               "short_name" : "Technopark Campus",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Kazhakkoottam",
               "short_name" : "Kazhakkoottam",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Thiruvananthapuram",
               "short_name" : "TVM",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Thiruvananthapuram",
               "short_name" : "TVM",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Kerala",
               "short_name" : "Kerala",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Technopark, Trivandrum Bypass Rd, Technopark Campus, Kazhakkoottam, Thiruvananthapuram, Kerala, India",
         "geometry" : {
            "location" : {
               "lat" : 8.55723570,
               "lng" : 76.87649309999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 8.558584680291503,
                  "lng" : 76.87784208029149
               },
               "southwest" : {
                  "lat" : 8.555886719708498,
                  "lng" : 76.87514411970848
               }
            }
         },
         "types" : [ "bus_station", "transit_station", "establishment" ]
      }
   ],
   "status" : "OK"
}

we get all the information about the location including latitude and longitude as a json array.

We can generalize the geocode request to use with PHP to get dynamic geocoding as follows,

$Addr = str_replace(" ", "+", $Addr);
$url='http://maps.googleapis.com/maps/api/geocode/json?address='.$Addr.'&sensor=false';
$json = file_get_contents($url,0,null,null); $json_output = json_decode($json);

echo $lat = $json_output->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
echo $long = $json_output->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}; 

Follow Mohammed Safeer On twitter.

Advertisements

2 thoughts on “Geocoding with Google Maps

  1. Pingback: Adding Bing Interactive Maps to Your Websites | TechLog

Leave a Reply

Please log in using one of these methods to post your comment:

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