我正在尝试通过Google maps API的json_decode获取信息,但由于某种原因,我得到一个奇怪的输出。有人可以帮我这个吗?
$request = file_get_contents ('https://maps.googleapis.com/maps/api/geocode/json?address=meerpaal%208d%20oosterhout&sensor=true');
$data = json_decode( $request, true );
foreach ( $data['results'][0]['geometry'] as $value ) {
echo $value['lat'] . '<br />';
echo $value['lng'] . '<br />';
}
这是我的输出:
51.6303446
4.8655305
R
R
看起来脚本的第一个字母是:
"location_type" : "ROOFTOP",
如果您观看此内容,接下来会发生什么:https://maps.googleapis.com/maps/api/geocode/json?address=meerpaal%208d%20oosterhout&sensor=true
我不需要2 R ..有谁知道我做错了什么?
编辑:
var_dump($data['results'][0]['geometry'])
Array
(
[location] => Array
(
[lat] => 51.6303446
[lng] => 4.8655305
)
[location_type] => ROOFTOP
[viewport] => Array
(
[northeast] => Array
(
[lat] => 51.631693580291
[lng] => 4.8668794802915
)
[southwest] => Array
(
[lat] => 51.628995619708
[lng] => 4.8641815197085
)
)
)
答案 0 :(得分:1)
晚会,但是我想添加一些对我有用的东西,因为我早期使用谷歌地图进行了高低搜索,以及如何动态收集纬度和长度与OP描述的方式相同。如果我不正确,请纠正我,因为我仍在学习PHP和谷歌地图,但同样,这段代码适用于我的需求,我想与OP和其他可能正在搜索适合的工作代码示例的人分享OPs标准。
我在敲击键盘之后创建了这个函数,这在PHP 7中适用于我。如果你收集一个地址geo位置,我认为不需要循环。
我已经提供了一个示例,说明我如何收集通过简单街道地址表单注册的Lat和Long客户。
*#注意:我没有显示在此答案中清除已发布变量的任何示例。请确保清除不需要的html实体/斜杠/空格等的帖子...... *
function get_lat_and_long($address){
// $address = The address string in a proper address format, we run through urlencode();
// Get JSON results from this request
// APIKEY = *Google API personal key* and is a constant coming from an external constants.php file.
// You can add your key statically though I recommend saving all sensitive variables/constants and arrays in a server side file that has no access by regular users.
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
// Convert JSON and validate
$geo = json_decode($geo, true);
if ($geo['status'] == 'OK') {
// Get Lat & Long
$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}else{
$latitude = NULL;
$longitude = NULL;
}
return $latitude;
return $longitude;
}
没有功能......
//create string holding address to run through google geolocation API for Lat/Long
$address = $_POST['usr_street'].' '.$_POST['usr_city'].' '.$_POST['usr_state'].', '.$_POST['usr_zip'];
// Get JSON results from this request
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY);
// Convert JSON
$geo = json_decode($geo, true);
if ($geo['status'] == 'OK') {
// Get Lat & Long
$latitude = $geo['results'][0]['geometry']['location']['lat'];
$longitude = $geo['results'][0]['geometry']['location']['lng'];
}else{
$latitude = NULL;
$longitude = NULL;
}
从这里开始,我可以将$latitude
和$longitude
推送到我的数据库阵列中,并将值保存在我的数据库列中。核实。
EX。
$userData = array(
'usr_first_name' => $_POST['usr_first_name'],
'usr_last_name' => $_POST['usr_last_name'],
'usr_password' => password_hash($_POST['usr_password'], PASSWORD_DEFAULT),
'usr_email' => $_POST['usr_email'],
'usr_company_name' => $_POST['usr_company_name'],
'usr_website' => $_POST['usr_website'],
'usr_street' => $_POST['usr_street'],
'usr_city' => $_POST['usr_city'],
'usr_state' => $_POST['usr_state'],
'usr_zip' => $_POST['usr_zip'],
'usr_phone1' => $_POST['usr_phone1'],
'usr_lat' => $latitude,
'usr_long' => $longitude,
//->etc...etc...etc...
);
//->$user is user object declared further up in document and comes from external user class -> user.class.php
//-> insert is function from external USER class that inserts query into DB.
$insert = $user->insert($userData);
希望这可以帮助其他正在使用PHP解决Google Maps API地理位置问题的人。在使用从表单条目构造的简单地址字符串动态收集Lat和Long方面。