谷歌地图API问题
我有一些代码可以接受任何地址并返回纬度和经度作为 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 ", "地址": "美国纽约州纽约", "AddressDetails": { "准确性" : 4, "国家" : { "行政区域" : { "AdministrativeAreaName" : "NY", "SubAdministrativeArea" : { "Locality" : { "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", " CountryNameCode" : "美国" } }, "扩展数据": { "LatLonBox": { “北”:40.8495342,“南”:40.5788964,“东”:-73.7498543,“西”:-74.2620919 } },“点”:{“坐标”:[-74.0059731, 40.7143528, 0 ] } }, { “id”:“p2”,“地址”:“美国纽约州曼哈顿”,“AddressDetails”:{“准确性”:4,“国家/地区”:{“管理区域”:{“管理区域名称”:“纽约", "子行政区域" : { "地区" : { "DependentLocality" : { "DependentLocalityName" : "曼哈顿" }, "LocalityName" : "纽约" }, "SubAdministrativeAreaName" : "纽约" } }, "CountryName" : "美国", "CountryNameCode" : "美国" } }, "扩展数据": { " LatLonBox": { "北": 40.8200450, "南": 40.6980780, "东": -73.9033130, "西": -74.0351490 } }, "点": { "坐标": [ -73.9662495, 40.7834345, 0 ] } } ] }
I've got some code that takes any address and returns lat and long as a php variable.
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>
I know it works because when I enter
http://maps.google.com/maps/geo?q=$whereurl
&output=csv&key=xxxxxxxxxxxxxxxxxxxx
manually into a browser, it returns
200,4,40.7143528,-74.0059731
which is what I need saved as a PHP variable. However it's not saving it as $north or $east. Any suggestions on how to fix this? Thank's in advance.
here's what it gives me:
{ "name": "new york new york", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "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": { "coordinates": [ -73.9662495, 40.7834345, 0 ] } } ] }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 file_get_contents 代替(它返回一个字符串)。
简短示例:
完整解决方案在这里:
http://www.ece.uvic .ca/~casualt/random.php?location=Bermuda
来源:
这应该可以满足您的要求。 :)
Use file_get_contents instead (which returns you a string).
Short Example:
Full Solution here:
http://www.ece.uvic.ca/~casualt/random.php?location=Bermuda
Source:
That should do the trick for you. :)