【发布时间】: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";?>° North, <?php echo "$east";?>° 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);并检查输出。