首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >google映射api和回声最近的地铁站名称

google映射api和回声最近的地铁站名称
EN

Stack Overflow用户
提问于 2018-03-09 19:13:14
回答 1查看 134关注 0票数 0

我正试图通过google获取最近地铁站的内容“名称”。

但由于某种原因我没有得到任何结果。

有什么想法吗?

代码语言:javascript
复制
<?
$stations = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=XXXXX&location=51.517238,-0.0731695&rankby=distance&type=subway_station');
$stations = json_decode($stations);

foreach ($stations as $data)
    {


echo  $data->results->name;

    }?>

** $STATIONS的打印/ VARDUMP **

代码语言:javascript
复制
object(stdClass)#113 (4) { ["html_attributions"]=> array(0) { } ["next_page_token"]=> string(468) "CrQCIQEAAIiUu7iBF_FuePInfUCwzG4ycBN9KTs3IQd0TF_VvYoz3ZHslqgGYANYyWPPcrtyLFjnHi6iBgbJqDTl972-fOLJsCJRhyKZsrXvx7I6RG9XBGFo4iVMqh10MVVjkCWSXKyW1M-vGneha2KLzQogqiGgYBLkG1dhyzlnaSmmpC0ur6rlzyTIRKQSewB79OjXi__cq7YuIcexmA3FqZHUqGtj2c8GL6X76wTeZWD37Rsl2WxC9FAsIqHQvxfOAySbf0xXtyWq6lxdBUt7rzbVRYfn5SrfQOapJGTe9Tx-ZculyJyMYEEC4pm4noVghQy0_LNgYCUxaiLjQv5_2DB4y5CPi8tDbLjAcHC7YOtAYEDYeIR7N5LAg2LDrXe378Z5zr0q8iLYmXqCFXoCJ9kwo4ISEFJHIBPTmrBDlEk9iCH8oEUaFBnA2CBg9vn9uYcNGZfFAQ50qyHk" ["results"]=> array(20) { [0]=> object(stdClass)#115 (11) { ["geometry"]=> object(stdClass)#116 (2) { ["location"]=> object(stdClass)#117 (2) { ["lat"]=> float(51.5151582) ["lng"]=> float(-0.0719412) } ["viewport"]=> object(stdClass)#118 (2) { ["northeast"]=> object(stdClass)#119 (2) { ["lat"]=> float(51.516427280292) ["lng"]=> float(-0.070816719708498) } ["southwest"]=> object(stdClass)#120 (2) { ["lat"]=> float(51.513729319709) ["lng"]=> float(-0.073514680291502) } } } ["icon"]=> string(73) "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png" ["id"]=> string(40) "47152d423eb61361555b2d8908e75c59d79deb1b" ["name"]=> string(12) "Aldgate East" ["photos"]=> array(1) { [0]=> object(stdClass)#121 (4) { ["height"]=> int(3966) ["html_attributions"]=> array(1) { [0]=> string(97) "Rene Lichtenstein" } ["photo_reference"]=> string(190) "CmRaAAAAPfCFksoZGq8FY3uKNCa271MIxH8dP7ZsE1lPnsvMep2OatQxY1SRN8LWxL4lRE_66vldeQbXnX7nrWg_bNLsGk9MYokXXq16ov1Hu01CSQe2nCqIQm6UzwcmjcqskSsDEhAIWrLuwREjELzxSkcPETQuGhSxU2Tai6pf8v4ugWTFDDz0amDAWA" ["width"]=> int(5950) } } ["place_id"]=> string(27) "ChIJ_7Q5M7UcdkgRCv-7nswC520" ["rating"]=> float(3.9) ["reference"]=> string(190) "CmRRAAAAqjpKiEO85qzXCVhdy9AhIyHZ2smVOnoDyGndzuW_s8G6yniXFcqGkdDTSA6xKZgLznbJUFbYRU3Kdth_wcx9aQXyvH7iua_zdt3PQ_Q-I3HstwRpW1nXDGXLFwcU6LVjEhD8CFwbm_VmLIpv820hobt3GhSQ9hZsbbGIoQ81bwfxTNHkMTKA6w" ["scope"]=> string(6) "GOOGLE" ["types"]=> array(4) { [0]=> string(14) "subway_station" [1]=> string(15) "transit_station" [2]=> string(17) "point_of_interest" [3]=> string(13) "establishment" } ["vicinity"]=> string(27) "Whitechapel High St, London" } [1]=> object(stdClass)#122 (11) { ["geometry"]=> object(stdClass)#123 (2) { ["location"]=> object(stdClass)#124 (2) { ["lat"]=> float(51.5142301) ["lng"]=> float(-0.0755928) } ["viewport"]=> object(stdClass)#125 (2) { ["northeast"]=> object(stdClass)#126 (2) { ["lat"]=> float(51.515579080292) ["lng"]=> float(-0.074243819708498) } ["southwest"]=> object(stdClass)#127 (2) { ["lat"]=> float(51.512881119709) ["lng"]=> float(-0.076941780291502) } } } ["icon"]=> string(73) "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-09 19:26:51

运行print_r($stations);将返回一个错误:

代码语言:javascript
复制
stdClass Object ( [error_message] => The provided API key is invalid. [html_attributions] => Array ( ) [results] => Array ( ) [status] => REQUEST_DENIED )

看上去这个问题以前就有过,但是如果不像我那样打印站点变量的话,这个问题就不会被问到。以下是一个指向谷歌(Google)所需商业账户的答案的链接:

google maps api, The provided API key is invalid

此外,参考Google文档,您的URL可能被错误地格式化:

https://developers.google.com/maps/documentation/javascript/error-messages

如果这有帮助,请告诉我。

编辑:

以下是另一个可能有帮助的答案:

Google Places API Web Services API Key Invalid

您可以将`key=XXXXX‘替换为按照https://developers.google.com/places/web-service/get-api-key上的说明生成的键

编辑:

这是一个要解析的数据日志,但在我看来,$data->results数组应该是您要迭代的那个。是否尝试将上下文更改为:

代码语言:javascript
复制
<?php
$stations = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=XXXXX&location=51.517238,-0.0731695&rankby=distance&type=subway_station');
$stations = json_decode($stations);
$stations = $stations->results;
foreach ($stations as $data)
    {

echo  $data->name;

    }

老实说,拥有API密钥的人可能在回答这个问题方面做得更好,但这是我最好的猜测,因为没有可解析格式的数据可以读取。并不是说它可以完成,只需要做大量的工作,我认为这应该从我读取var转储的方式中起作用。

最终编辑

我可以确认上面的代码有效。当在for循环中使用echo <br>;打印结果时,结果将产生:

代码语言:javascript
复制
Aldgate East
Aldgate Station
Liverpool Street
Tower Hill
Whitechapel
Moorgate Station
Monument Station
Bank Underground Station
Old Street
Old Street
Cannon Street Underground Station
Mansion House Underground Station
London Bridge Underground Station
Bethnal Green
St. Paul's
Barbican Station
Stepney Green Underground Station
Blackfriars
Farringdon
Bermondsey
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49200610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档