PHP - Get Latitude & Longitude from an address

I have used the autocomplete of geocode but when I am selecting from the drop down it is giving me the lat and long of the address

I need to check when the lat and long are empty then from the posted address I must get the latitude and longitude


suppose you have hidden value for lat and long is mapLat & mapLong and input field name is location then you can check as below

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

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("$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;

Try This for getting the address

function getaddress($lat,$lng)
$url = ''.trim($lat).','.trim($lng).'&sensor=false';
$json = @file_get_contents($url);
$status = $data->status;
return $data->results[0]->formatted_address;
return false;

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


Using CURL

$address = "Kathmandu, Nepal";
$url = "".urlencode($address);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$responseJson = curl_exec($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;

// We define our address
$address = 'Indore, MP 452001';

// function to get  the address
function get_lat_long($address) {
   $array = array();
   $geo = file_get_contents(''.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;

Need Your Help

Password validation in UITextField in iOS

objective-c validation ios5 passwords uitextfield

I have 1 UITextfield for password in my iPhone application.

Restoring MySQL database from physical files

mysql backup restore

Is it possible to restore a MySQL database from the physical database files. I have a directory that has the following file types: