简体   繁体   中英

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?

Below code worked for me

 <?php
    $zipcode="92604";
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
    $details=file_get_contents($url);
    $result = json_decode($details,true);

    $lat=$result['results'][0]['geometry']['location']['lat'];

    $lng=$result['results'][0]['geometry']['location']['lng'];

    echo "Latitude :" .$lat;
    echo '<br>';
    echo "Longitude :" .$lng;
    ?>

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
With the curl you can get lat and long from below code:

$zipcode=90012;
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zipcode)."&sensor=false&key=XXXXXXXX";
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    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;
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM