2014-04-22 14 views
12

Użyłem autouzupełniania Geokodując ale gdy jestem wybierając z rozwijanego to daje mi lat i długo adresuPHP - Get szerokość i długość geograficzną z adresu

muszę sprawdzić kiedy łac i długo są puste, a następnie z zarachowania adres muszę uzyskać szerokość i długość geograficzną

+0

Co cię trapi? Z której części dokładnie potrzebujesz pomocy? (Zobacz także [Zapytaj] o wskazówki dotyczące zadawania lepszych pytań) –

Odpowiedz

26

Załóżmy, że masz ukryte wartości dla lat i długo mapLat & mapLong i nazwa pola wejściowe jest lokalizacja następnie można sprawdzić, jak poniżej

<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

cześć, chciałbym zapytać, czy mogę użyć tego kodu w php bez uzyskania klucza API, czy to w porządku? – Will

+0

nie będzie działać. Musisz użyć api do uzyskania wyniku – sandipshirsale

+0

Dzięki za to! Wyszukiwano ponad godzinę dla działającego kodu. to działa dla mnie doskonale. – Harsha

3

Spróbuj to na uzyskanie adresu

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

ma dokładnie odwrotny adres. – 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

KorzystanieCURL

<?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); 
}  
?> 
Powiązane problemy