Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 932
khan Asim Avatar asked Apr 22 '14 07:04

khan Asim


People also ask

How get lat long from address Google Maps API PHP?

function getaddress($lat, $lng) { $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . trim($lat) . ',' .

How to convert address to coordinates in PHP?

Function Usage: php $address = 'Brooklyn, NY, USA'; $address = str_replace(' ', '+', $address); $result = getGeoCode($address); echo 'Latitude: ' . $result['lat'] . ', Longitude: ' . $result['lng']; // produces output // Latitude: 40.6781784, Longitude: -73.9441579 ?>


3 Answers

Suppose you have hidden value for lat and long is mapLat & mapLong and input field name is location then:

<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;
}
like image 189
sandipshirsale Avatar answered Oct 28 '22 01:10

sandipshirsale


Using CURL

<?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);
}    
?>
like image 10
Mukesh Chapagain Avatar answered Oct 28 '22 02:10

Mukesh Chapagain


I tried above solutions but none was working then i tried to fix on my own so below is the correct code:

    $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;
like image 5
FahadKhan Avatar answered Oct 28 '22 02:10

FahadKhan