谷歌地图API麻烦

时间:2012-02-24 20:45:51

标签: php geocoding

我有一些代码可以获取任何地址并返回lat和long作为php变量。

if ($_SESSION['where']) {
    $where = stripslashes($_SESSION['where']);
    $whereurl = urlencode($where);

$location = file("http://maps.google.com/maps/geo?q=new+york+New+york
&output=csv&key=xxxxxxxxxxxxxxxxxxxxxxxx");
        list ($stat,$acc,$north,$east) = explode(",",$location[0]);
        $html = "Information for ".$where."<br>";
        $html .= "North: $north, East: $east<br>";
        $html .= "Accuracy: $acc, Status: $stat<br>";
} else {
        $html = "Varibles Undefined";
}

?>


     <?php $_SESSION['lat'] = "$html"; $_SESSION['lon'] = "$whereurl"; echo"<strong>"?><?php echo "$north";?>&deg; North, <?php echo "$east";?>&deg; East</strong>

我知道它有效,因为当我进入

http://maps.google.com/maps/geo?q=$whereurl
    &output=csv&key=xxxxxxxxxxxxxxxxxxxx

手动进入浏览器,返回

200,4,40.7143528,-74.0059731

这是我需要保存的PHP变量。然而,它并没有将其保存为$ north或$ east。对于如何解决这个问题,有任何的建议吗?提前谢谢。

这是它给我的东西:

{“name”:“new york new york”,“Status”:{“code”:200,“request”:“geocode”},“Placemark”:[{“id”:“p1”,“地址“:”New York,NY,USA“,”AddressDetails“:{”Accuracy“:4,”Country“:{”AdministrativeArea“:{”AdministrativeAreaName“:”NY“,”SubAdministrativeArea“:{”Locality“: {“LocalityName”:“New York”},“SubAdministrativeAreaName”:“New York”}},“CountryName”:“USA”,“CountryNameCode”:“US”}},“ExtendedData”:{“LatLonBox”:{ “north”:40.8495342,“south”:40.5788964,“east”: - 73.7498543,“west”: - 74.2620919}},“Point”:{“coordinates”:[ - 74.0059731,40.7143528,0]}},{“ id“:”p2“,”address“:”Manhattan,New York,NY,USA“,”AddressDetails“:{”Accuracy“:4,”Country“:{”AdministrativeArea“:{”AdministrativeAreaName“:”NY“ ,“SubAdministrativeArea”:{“Locality”:{“DependentLocality”:{“DependentLocalityName”:“Manhattan”},“LocalityName”:“New York”},“SubAdministrativeAreaName”:“New York”}},“CountryName”: “USA”,“CountryNameCode”: “US”}},“ExtendedData”:{“LatLonBox”:{“north”:40.8200450,“south”:40.6980780,“east”: - 73.9033130,“west”: - 74.0351490}},“Point”:{“坐标“:[ - 73.9662495,40.7834345,0]}}]}

1 个答案:

答案 0 :(得分:0)

使用file_get_contents(返回一个字符串)。

简短示例:

$location = file_get_contents("http://maps.google.com/maps/geo?q=$whereurl&output=csv");
$test = explode(",",$location);
print_r($test);

完整解决方案:

http://www.ece.uvic.ca/~casualt/random.php?location=Bermuda

来源:

$theLocation = "new+york";
$locIn = $_GET['location'];
if($locIn)
{
    $theLocation = urlencode($locIn);   //Just to be safe (even though this got passed in via get
}
$location = file_get_contents('http://maps.google.com/maps/geo?q='.$theLocation.'&output=csv&key=AIzaSyAQtiJHUt5KiL-aGONLlACQQ3OdwpbvFzI');
$test = explode(",",$location);
//print_r($test);
$lat = $test[2];
$long = $test[3];
echo '<h1>Magical Location of '.$theLocation.':</h1>';
echo '<ul>';
echo '<li>Longitude: '.$long.'</li>';
echo '<li>Latitude: '.$lat.'</li>';
echo '</ul>';

那应该为你做的伎俩。 :)