使用 PHP 解析 Google Geocoding JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10735662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:54:51  来源:igfitidea点击:

Parsing Google Geocoding JSON with PHP

phparraysjsongoogle-mapsgeocoding

提问by Lee Price

I'm trying to parse the json response from the Google Geocode API but I'm having a little trouble understanding it.

我正在尝试解析来自 Google Geocode API 的 json 响应,但我在理解它时遇到了一些麻烦。

For those unfamiliar with the Geocode API here is the URL: http://maps.google.com/maps/api/geocode/json?address=albert%20square&sensor=false

对于那些不熟悉 Geocode API 的人,这里是 URL:http://maps.google.com/maps/api/geocode/json?address=albert%20square&sensor=false

I'm using the following code to parse the request

我正在使用以下代码来解析请求

<?php

$address = urlencode($_POST['address']);
$request = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . $address . "&sensor=false");
$json = json_decode($request, true);

?>

And I'm trying to output with the following:

我正在尝试输出以下内容:

echo $json['results'][0]['formated_address'];

I'm not sure why nothing is being echoed. I've also tried $json[0]['results'][0]['formated_address']. I know this is a noob question, but multi-dimensional arrays confuse me.

我不知道为什么没有任何回应。我也试过了$json[0]['results'][0]['formated_address']。我知道这是一个菜鸟问题,但多维数组让我感到困惑。

采纳答案by DaveRandom

echo $json['results'][0]['formatted_address'];

It helps if you spell it correctly ;-)

如果你拼写正确,它会有所帮助;-)

回答by Milan

...just FYI, the JSON request URL has to be properly formatted in order to return anything good otherwise you may get NULL response.

...仅供参考,JSON 请求 URL 必须正确格式化才能返回任何好的内容,否则您可能会得到 NULL 响应。

For example, this is how I retrieve longitude and latitudevalues from the JSON response:

例如,这是我从 JSON 响应中检索经度和纬度值的方式:

// some address values
    $client_address = '123 street';
    $client_city = 'Some City';
    $client_state = 'Some State';
    $client_zip = 'postal code';

// building the JSON URL string for Google API call 
    $g_address = str_replace(' ', '+', trim($client_address)).",";
    $g_city    = '+'.str_replace(' ', '+', trim($client_city)).",";
    $g_state   = '+'.str_replace(' ', '+', trim($client_state));
    $g_zip     = isset($client_zip)? '+'.str_replace(' ', '', trim($client_zip)) : '';

$g_addr_str = $g_address.$g_city.$g_state.$g_zip;       
$url = "http://maps.google.com/maps/api/geocode/json?
        address=$g_addr_str&sensor=false";

// Parsing the JSON response from the Google Geocode API to get exact map coordinates:
// latitude , longitude (see the Google doc. for the complete data return here:
// https://developers.google.com/maps/documentation/geocoding/.)

$jsonData   = file_get_contents($url);

$data = json_decode($jsonData);

$xlat = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$xlong = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

echo $xlat.",".$xlong;

...also, you can use the same code to hard-code the Google Map iframe and embed it into you page if the v3 API does not behave... see simple tutorial here: http://pmcds.ca/blog/embedding-google-maps-into-the-webpage.html

...此外,如果 v3 API 不起作用,您可以使用相同的代码对 Google Map iframe 进行硬编码并将其嵌入到您的页面中...请参阅此处的简单教程:http: //pmcds.ca/blog/嵌入-google-maps-into-the-webpage.html

Embedding the is not exactly the right approachbut there are times when it becomes needed solution.

嵌入 并不是完全正确的方法,但有时它会成为需要的解决方案。