2009-10-06 9 views

Odpowiedz

4

Korzystanie z Google Map API i PHP, aby uzyskać współrzędne:

$url = 'http://maps.google.com/maps/geo?q='.$address.'&output=json&oe=utf8&sensor=false&key='.$api_key; 
$data = @file_get_contents($url); 
$jsondata = json_decode($data,true); 
if(is_array($jsondata)&& $jsondata ['Status']['code']==200){ 
    $lat = $jsondata ['Placemark'][0]['Point']['coordinates'][0]; 
    $lon = $jsondata ['Placemark'][0]['Point']['coordinates'][1]; 
} 
8

I odwrotnie:

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

$output= json_decode($geocode); 

$lat = $output->results[0]->geometry->location->lat; 
$long = $output->results[0]->geometry->location->lng; 
3

tutaj jest moja klasa:

class GeoCoder { 
public static function getLatLng($address){ 
    try{ 
     $address = urlencode($address); 
     $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false'); 

     $output= json_decode($geocode); 

     $lat = $output->results[0]->geometry->location->lat; 
     $lng = $output->results[0]->geometry->location->lng; 
     if(is_numeric($lat) && is_numeric($lng)){ 
      return array("lat" => $lat, "lng" => $lng); 
     } 
    } 
    catch(Exception $e){    
     return false; 
    } 
} 

}

Powiązane problemy