Lee*_*ice 15 php arrays json google-maps geocoding
我正在尝试解析来自Google Geocode API的json响应,但我在理解它时遇到了一些麻烦.
对于那些不熟悉Geocode API的人,请访问以下网址:http://maps.google.com/maps/api/geocode/json?address = alb%20square&_sensor = false
我正在使用以下代码来解析请求
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
我正在尝试输出以下内容:
echo $json['results'][0]['formated_address'];
Run Code Online (Sandbox Code Playgroud)
我不确定为什么没有回应.我也试过了$json[0]['results'][0]['formated_address'].我知道这是一个noob问题,但是多维数组让我很困惑.
Dav*_*dom 14
echo $json['results'][0]['formatted_address'];
Run Code Online (Sandbox Code Playgroud)
如果你正确拼写它会有所帮助;-)
...仅供参考,JSON请求URL必须正确格式化才能返回任何好的内容,否则您可能会收到NULL响应.
例如,这是我从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;
Run Code Online (Sandbox Code Playgroud)
...另外,如果v3 API不起作用,您可以使用相同的代码对Google Map iframe进行硬编码并将其嵌入到您的页面中...请参阅此处的简单教程:http://pmcds.ca/blog/嵌入-谷歌-地图-到最webpage.html
嵌入 不是正确的方法,但有时它成为需要的解决方案.
| 归档时间: |
|
| 查看次数: |
20258 次 |
| 最近记录: |