dsv38843 2018-03-09 19:13
浏览 54
已采纳

谷歌地图api foreach和呼应最近的地铁站名称

I am trying to get the contents "name" of the nearest subway stations via the google maps api.

But i'm getting no results for some reason.

any ideas?

<?
$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;

    }?>

**PRINT / VARDUMP OF $STATIONS **

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"  
  • 写回答

1条回答 默认 最新

  • dtgr3392 2018-03-09 19:26
    关注

    Running print_r($stations); returns an error:

    stdClass Object ( [error_message] => The provided API key is invalid. [html_attributions] => Array ( ) [results] => Array ( ) [status] => REQUEST_DENIED )
    

    It looks like this issue has been seen before, but wasn't asked in a way that could have been answered without printing the stations variable the way I did. Here is a link to the answer that refers to a required business account with Google:

    google maps api, The provided API key is invalid

    Also, referring to the Google documentation, it looks like your URL may be formatted incorrectly:

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

    Please let me know if this helps.

    EDIT:

    Here is another answer that might help:

    Google Places API Web Services API Key Invalid

    You can replace `key=XXXXX' with a key generated following the instructions at https://developers.google.com/places/web-service/get-api-key

    EDIT:

    That is a log of data to parse, but it looks to me that the $data->results array should be the one you are iterating through. Have you tried changing the context to:

    <?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;
    
        }
    

    Honestly, someone with an API key might do better at answering this question, but that's my best guess without having the data in a parsable format to read back in. Not saying it can be done, just take a lot of work, and I think this should work from how I read the var dump.

    FINAL EDIT

    I can confirm the above code works. The results when printed with echo <br>; in the for loop will result in:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改