2014-04-22 13 views
12

ho usato il completamento automatico di geocodifica, ma quando sto selezionando dal menu a tendina che mi sta dando il lat e lungo dell'indirizzoPHP - Get latitudine e longitudine da un indirizzo

ho bisogno di controllare quando il lat e lunga sono vuoti, allora l'indirizzo postato devo ottenere la latitudine e la longitudine

+0

Cosa ti preoccupa? Con quale parte hai esattamente bisogno di aiuto? (Vedi anche [Chiedi] per suggerimenti su come fare domande migliori) –

risposta

26

supponiamo di avere nascosto rapporto qualità-lat e lungo è mapLat & mapLong e il nome del campo di input è la posizione allora si può verificare, come di seguito

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



extact($_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; 
} 
+0

ciao, vorrei chiedere se uso questo codice in php senza ottenere una chiave API, va bene? – Will

+0

no, non funzionerà. Devi usare l'API per ottenere il risultato – sandipshirsale

+0

Grazie per questo! Ho cercato più di un'ora per lavorare codice. questo funziona per me perfettamente. – Harsha

3

Prova questo per ottenere l'indirizzo

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

 
?>

+0

fa l'esatto opposto. – Gogol

2
// 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; 
} 
2

UtilizzandoCURL

<?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); 
}  
?> 
Problemi correlati