How can i get latitude and longitude from php pincode?

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?

asked Dec 20, 2011 at 7:34

KichuKichu

3,28415 gold badges67 silver badges134 bronze badges

2

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;
    ?>

answered Aug 1, 2013 at 7:22

How can i get latitude and longitude from php pincode?

MufaddalMufaddal

5,3707 gold badges43 silver badges57 bronze badges

1

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

answered Dec 20, 2011 at 9:35

Sudhir BastakotiSudhir Bastakoti

97.8k15 gold badges156 silver badges161 bronze badges

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;
    

answered Oct 8, 2021 at 6:05

    Table of contents
  • Getting Latitude And Longitude From Pin Code Using PHP
  • Get latitude and longitude automatically using php, API
  • PHP - Get Latitude & Longitude from an address [closed]
  • How to get latitude and longitude from address in php code example
  • Get latitude and longitude from zip code using PHP

Getting Latitude And Longitude From Pin Code Using PHP

$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;
 <?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;
    ?>
$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;
    

Get latitude and longitude automatically using php, API

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
$address = str_replace(" ", "+", $address);
$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
//add urlencode to your address
$address = urlencode("technopark, Trivandrun, kerala,India");
$region = "IND";
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");

echo $json;

$decoded = json_decode($json);

print_r($decoded);
$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
$address = "Salzburg";
$address = str_replace(" ", "+", $address);
$region = "Austria";

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
echo $lat."</br>".$long;

PHP - Get Latitude & Longitude from an address [closed]

<html>
<form>
<input type="hidden" name="mapLat">
<input type="hidden" name="mapLong">
<input type="text" name="location">
<input type="submit" name="submit" value="submit">
</form>
</html>



extract($_POST);
if($mapLat =='' && $mapLong ==''){
        // Get lat long from google
        $latlong    =   get_lat_long($location); // create a function with the name "get_lat_long" given as below
        $map        =   explode(',' ,$latlong);
        $mapLat         =   $map[0];
        $mapLong    =   $map[1];    
}


// function to get  the address
function get_lat_long($address){

    $address = str_replace(" ", "+", $address);

    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
    $json = json_decode($json);

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    return $lat.','.$long;
}
<?php
$address = "Kathmandu, Nepal";
$url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($address);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$responseJson = curl_exec($ch);
curl_close($ch);

$response = json_decode($responseJson);

if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;

    echo 'Latitude: ' . $latitude;
    echo '<br />';
    echo 'Longitude: ' . $longitude;
} else {
    echo $response->status;
    var_dump($response);
}    
?>
    $going=$this->input->post('going');

    $address =$going; // Google HQ
    $prepAddr = str_replace(' ','+',$address);
    $apiKey = 'Add your API Key'; // Google maps now requires an API key.

    $geocode=file_get_contents('https://maps.googleapis.com/maps/api/geocode/json? 
   address='.urlencode($address).'&sensor=false&key='.$apiKey);

    //print_r($geocode);

    $output= json_decode($geocode);
    $latitude = $output->results[0]->geometry->location->lat;
    $longitude = $output->results[0]->geometry->location->lng;
// We define our address
$address = 'Indore, MP 452001';
echo"<PRE>";
print_r(get_lat_long($address));

// function to get  the address
function get_lat_long($address) {
   $array = array();
   $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');

   // We convert the JSON to an array
   $geo = json_decode($geo, true);

   // If everything is cool
   if ($geo['status'] = 'OK') {
      $latitude = $geo['results'][0]['geometry']['location']['lat'];
      $longitude = $geo['results'][0]['geometry']['location']['lng'];
      $array = array('lat'=> $latitude ,'lng'=>$longitude);
   }

   return $array;
}
<?
function getaddress($lat, $lng)
{
  $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . trim($lat) . ',' . trim($lng) . '&sensor=false';
  $json = @file_get_contents($url);
  $data = json_decode($json);
  $status = $data->status;
  if ($status == "OK")
    return $data->results[0]->formatted_address;
  else
    return false;
}


$lat = 26.754347; //latitude
$lng = 81.001640; //longitude
$address = getaddress($lat, $lng);
if ($address) {
  echo $address;
} else {
  echo "Not found";
}

?>

How to get Latitude, Longitude and Postal Code From Address using PHP and Google geo api

function extractLatLngFromAdd($address) {
 try {
if(!isset($address) || empty($address)) {
   throw new exception("Address is not set.");
 }
 
$geodata =   json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false')); 
if(!$geodata) {
  throw new exception("Error to extract geodata.");
}
// extract latitude and longitude
$result['latitude']  = $geodata->results[0]->geometry->location->lat; 
$result['longitude'] = $geodata->results[0]->geometry->location->lng;
if(empty($result)) {
 throw new exception("Couldn't extract latitude and longitude, Plz try to input different address.");
}
return $result;
} catch (Exception $e) {
   return $e->getMessage();
}
}
 
print_r(extractLatLngFromAdd("barra kanpur"));
function extractPostalCodeFromAdd($address){
try {
if(!isset($address) || empty($address)) {
   throw new exception("Address is not set.");
 }
 
$geodata =   json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false')); 
if(!$geodata) {
  throw new exception("Error to extract geodata.");
}
// extract latitude and longitude
$lat  = $geodata->results[0]->geometry->location->lat; 
$lng = $geodata->results[0]->geometry->location->lng;
 
$geodata = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$lat.','.$lng.'&sensor=false')); 
if(!$geodata) {
  throw new exception("Error to extract geodata.");
}
// extracting postcode from address components
foreach($geodata->results[0]->address_components as $addAttr){
                if($addAttr->types[0] == 'postal_code'){
                    if(empty($addAttr->long_name) || !isset($addAttr->long_name)) {
                       throw new exception("Postal Codenot found, Plz try to input different address.");
                     }
                    return $addAttr->long_name;
 
                } 
            }
 } catch (Exception $e) {
   return $e->getMessage();
}
}
 
echo extractPostalCodeFromAdd("barra kanpur");

How to get latitude and longitude from address in php code example

<?php
$address = "Kathmandu, Nepal";
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&key=apikey';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$responseJson = curl_exec($ch);
curl_close($ch);

$response = json_decode($responseJson);

if ($response->status == 'OK') {
    $latitude = $response->results[0]->geometry->location->lat;
    $longitude = $response->results[0]->geometry->location->lng;

    echo 'Latitude: ' . $latitude;
    echo '<br />';
    echo 'Longitude: ' . $longitude;
} else {
    echo $response->status;
    var_dump($response);
}    
?>
var x=document.getElementById("demo");
function getLocation(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
    else{
        x.innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position){
    lat=position.coords.latitude;
    lon=position.coords.longitude;
    displayLocation(lat,lon);
}

function showError(error){
    switch(error.code){
        case error.PERMISSION_DENIED:
            x.innerHTML="User denied the request for Geolocation."
        break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML="Location information is unavailable."
        break;
        case error.TIMEOUT:
            x.innerHTML="The request to get user location timed out."
        break;
        case error.UNKNOWN_ERROR:
            x.innerHTML="An unknown error occurred."
        break;
    }
}

function displayLocation(latitude,longitude){
    var geocoder;
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude);

    geocoder.geocode(
        {'latLng': latlng}, 
        function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var add= results[0].formatted_address ;
                    var  value=add.split(",");

                    count=value.length;
                    country=value[count-1];
                    state=value[count-2];
                    city=value[count-3];
                    x.innerHTML = "city name is: " + city;
                }
                else  {
                    x.innerHTML = "address not found";
                }
            }
            else {
                x.innerHTML = "Geocoder failed due to: " + status;
            }
        }
    );
}
<!DOCTYPE html>

<html>

<form method="post">

<input type="text" name="address">

<input type="submit" name="submit" value="submit">

</form>

</html>

<?php

if(isset($_POST['submit']))

{


function getLatLong($address){

    if(!empty($address)){

        //Formatted address

        $formattedAddr = str_replace(' ','+',$address);

        //Send request and receive json data by address

        $geocodeFromAddr = file_get_contents
('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false'); 

        $output = json_decode($geocodeFromAddr);

        //Get latitude and longitute from json data

        $data['latitude']  = $output->results[0]->geometry->location->lat; 

        $data['longitude'] = $output->results[0]->geometry->location->lng;

        //Return latitude and longitude of the given address

        if(!empty($data)){

            return $data;

        }else{

            return false;

        }

    }else{

        return false;  
 
    }

}

$address = $_POST['address'];

$latLong = getLatLong($address);

$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';

$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';

echo "Latitude:".$latitude."<br>";

echo "longitude:".$longitude."";

}

?>

Get latitude and longitude from zip code using PHP

function getLnt($zip){
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&key=[YOUR KEY]";
$result_string = file_get_contents($url);
$result = json_decode($result_string, true);
$result1[]=$result['results'][0];
$result2[]=$result1[0]['geometry'];
$result3[]=$result2[0]['location'];
return $result3[0];
}
<?php
 $val = getLnt('90001');
 echo "Latitude: ".$val['lat']."<br>";
 echo "Longitude: ".$val['lng']."<br>";
?>

Next Lesson PHP Tutorial

How can I get latitude and longitude from ZIP code in php?

php function getLnt($zip){ $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false"; $result_string = file_get_contents($url); $result = json_decode($result_string, true); return $result['results'][0]['geometry']['location']; } getLnt("750341"); ?>

How do I find my latitude and longitude PIN code?

You can get postal code by using Google map's API. You can get the postal code from the json data returned by the API. Provide the lat lng to LatLng constructor.

How can I get current location using latitude and longitude in php?

how to get current location latitude and longitude in php.
function geoLocate($address).
$lat = 0;.
$lng = 0;.
$data_location = "https://maps.google.com/maps/api/geocode/json? key=".$ GOOGLE_API_KEY_HERE." &address=". str_replace(" ", "+", $address)." &sensor=false";.
$data = file_get_contents($data_location);.