【问题标题】:get variables from Google Maps with JSON使用 JSON 从谷歌地图获取变量
【发布时间】:2023-03-22 03:10:01
【问题描述】:

我在这里做错了什么?我想用谷歌地图从邮政编码中获取纬度和经度,这很容易,我得到了结果,但我似乎无法保存它们。这是我正在使用的。我只需要获取并保存lnglat,但似乎不止1个;我想要'位置'中的那个。

$get_pickup_coords = "https://maps.googleapis.com/maps/api/geocode/xml?&address=cv57bt&sensor=false";
$pickup_data = @file_get_contents($get_pick_coords);
$pickup_result = json_decode($pickup_data, true);   

$latitude = $pickup_result[3];

【问题讨论】:

    标签: php json google-maps-api-3


    【解决方案1】:

    您的脚本中有几个错误。对于初学者,您正在使用 xml 输出并尝试 json_decode 它。通过在 google api 中传递 json 参数而不是 xml 来更正此问题。

    其次,$pickup_result[3]不会给你lat longs,如下图所示:

    $get_pickup_coords = "https://maps.googleapis.com/maps/api/geocode/json?&address=cv57bt&sensor=false";
    $pickup_data = file_get_contents($get_pickup_coords);
    $pickup_result = json_decode($pickup_data, true);   
    $lat = $pickup_result['results'][0]['geometry']['location']['lat'];
    $long = $pickup_result['results'][0]['geometry']['location']['lng'];
    

    $lat$long 变量分别包含纬度和经度。

    【讨论】:

    • 不客气。你也可以投票给答案吗:)
    【解决方案2】:
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=cv57bt";
    $data = json_decode( file_get_contents( $url ) );
    $location=$data->results[0]->geometry->location;
    
    echo 'Latitude:',$location->lat,' Longitude:',$location->lng;
    

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 2012-01-23
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2016-12-23
      相关资源
      最近更新 更多