Search code examples
phpgoogle-apilatitude-longitude

getting Latitude And Longitude From Pin Code Using PHP


In my site i want to get the latitude and longitude of given pin code or postal code. If i give 680721 as pin code or postal code then i want to get its corresponding latitude and longitude using php code. How can i do this? If this is possible in PHP? I have php code to get latitude and longitude of given name of place.

$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$exp_pjctloc[$i].'&sensor=false';
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$lat =  $search_data->results[0]->geometry->location->lat;
$lng =  $search_data->results[0]->geometry->location->lng;

But how can i get it by using pin code or postal code?


Solution

  • This worked for me:

    $zip = 94043;
    $site = file_get_contents('http://geocoder.ca/?postal='.$zip, false, NULL, 1000, 1000);
    $goods = strstr($site, 'GPoint('); // cut off the first part up until 
    $end = strpos($goods, ')'); // the ending parenthesis of the coordinate
    $cords = substr($goods, 7, $end - 7); // returns string with only the 
    $array = explode(', ',$cords); // convert string into array
    echo "<pre>";
    print_r($array); // output the array to verify