0

I am quite new to JavaScript and I am looking for a basic API or function that I can feed an address as a string value and it returns me the longitude and latitude of that address. I have a series of addresses in a DB and I want to add the longitude and latitude values for each of them.

Thanks

aHunter
  • 3,490
  • 11
  • 39
  • 46
  • I'm assuming you mean a postal-style address, not an IP or something, right? – Bojangles Aug 16 '11 at 11:04
  • possible duplicate of [How to obtain longitude and latitude for a street address programmatically (and legally)](http://stackoverflow.com/questions/158474/how-to-obtain-longitude-and-latitude-for-a-street-address-programmatically-and-l) – Gordon Aug 16 '11 at 11:05

3 Answers3

1

This is called Geocoding and is quite easy to do with the Google Maps API:

http://code.google.com/apis/maps/documentation/geocoding/#GeocodingRequests

evilcelery
  • 15,941
  • 8
  • 42
  • 54
1

Find below example using php

<?php
$address ="1600+Amphitheatre+Parkway,+Mountain+View,+CA";
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=false";
$getAddress = simplexml_load_file($url);
print"<pre>";
print_r($getAddress);
?>

It return all detail you need to get latitude/longitude as below.

<?php
print"<pre>";
print_r((string)$getAddress->result->geometry->location->lat); echo "<br />";
print_r((string)$getAddress->result->geometry->location->lng);
?>

For more detail please click on below link:

http://code.google.com/apis/maps/documentation/geocoding/

Sanjeev Chauhan
  • 3,977
  • 3
  • 24
  • 30
0
   function GetLatLong($address){
    $myKey = 'Get key from google api v2 ';
        $URL = "http://maps.google.co.uk/maps/geo?q=" . urlencode($address) . "&output=xml&key=".$myKey;
        $xml = simplexml_load_file($URL);
        $status = $xml->Response->Status->code;
        if ($status=='200') 
        { //address geocoded correct, show results
            foreach ($xml->Response->Placemark as $node) 
            { // loop through the responses
                $address = $node->address;
                if(strpos($address,'USA') || strpos($address,'usa'))
                {
                    $quality = $node->AddressDetails['Accuracy'];
                    $coordinates = $node->Point->coordinates;
                    return (explode(',',$coordinates));
                }
            }
        }
    }

This api will only work for usa and uk addresses . The address should be separated with '+' and should not contain any spaces. This code is only parsing coordinates for usa . Please modify the code according to your requirement

Rahul Kanodia
  • 105
  • 12