【问题标题】:Google Map API Trouble谷歌地图 API 问题
【发布时间】:2012-03-15 07:38:26
【问题描述】:

我有一些代码可以接受任何地址并返回 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", "地址”:“纽约,纽约,美国”,“地址详细信息”:{“准确度”:4,“国家”:{“行政区域”:{“行政区域名称”:“纽约”,“子行政区域”:{“区域”: { "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { “北”:40.8495342,“南”:40.5788964,“东”:-73.7498543,“西”:-74.2620919 } },“点”:{“坐标”:[-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”:“纽约”},“SubAdministrativeAreaName”:“纽约”}},“CountryName”: “美国”、“国名代码”: "US" } }, "ExtendedData": { "LatLonBox": { "north": 40.8200450, "south": 40.6980780, "east": -73.9033130, "west": -74.0351490 } }, "Point": { "坐标”:[-73.9662495, 40.7834345, 0] } } ] }

【问题讨论】:

  • 它与javascript有什么关系? manually into a browser, it retruns 可能前面有一个空行。尝试用file_get_contents 替换file 并在$location 之后删除[0]
  • @Cheery 做了这两件事,仍然没有注册这两个变量。
  • 在这种情况下,我们需要谷歌地图请求的示例。或者告诉我们var_dump($location);的结果
  • 这是一个请求 url 的示例 maps.google.com/maps/… 是您的意思吗?
  • 而且效果很好。根据请求检查脚本中实际包含的内容,在 file 行之后插入 var_dump($location); 并检查输出。

标签: php geocoding


【解决方案1】:

改用 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>';

这应该对你有用。 :)

【讨论】:

  • 仍然无法正常工作...当我回显 $html 时,它会为 New york、New york: 、East: Accuracy: 、Status: 和 Session Where 设置正确的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
相关资源
最近更新 更多