Google 地图 api 地理编码不起作用
Google maps api geocoding not working
我正在 CodeIgniter 中开发一个项目,我需要特定位置的 lat
和 lng
。底部的代码工作正常,但现在 $geo['status']
显示 QUERY_OVER_LIMIT
。如何解决?
我还收到以下警告:
undefined offset:0 error in $latlng['lat']= $geo['results'][0]['geometry']['location']['lat']; $latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];
提前致谢。这是我的代码:
$address = $this->input->post('key');
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geoco/json?address='.urlencode($address).'&sensor=false');
$geo = json_decode($geo, true);
if ($geo['status'] = 'OK') {
$latlng['lat']= $geo['results'][0]['geometry']['location']['lat'];
$latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];
echo json_encode($latlng);
} else {
echo 0;
}
首先,您对 $geo['status'] = 'OK'
的检查将始终是 'OK',因为您使用了一个“=”而不是“==”。
这意味着即使 google 响应与您预期的不同,您仍然继续进入该代码块。
<?php
$row = [
'address' => '1315 10th St.',
'city' => 'Sacramento',
'state' => 'CA',
'zip' => '95814'
];
$address = str_replace(' ', '+', $row['address'] ) . ','
. str_replace(' ', '+', $row['city'] ) . ','
. str_replace(' ', '+', $row['state'] ) . ','
. str_replace(' ', '+', $row['zip'] );
function get_gps_coordinates_of_street_address( $address )
{
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address='
. $address;
if( $response = @file_get_contents( $url ) )
{
$geo = json_decode( $response, TRUE );
if( $geo['status'] == 'OK' )
{
return $geo['results'][0]['geometry']['location']['lat'] . ',' . $arr['results'][0]['geometry']['location']['lng'];
}
else
{
return 'GPS coordinates were not available for supplied ADDRESS';
}
}
else
{
return 'GPS coordinates were not available because connection failed or malformed request';
}
}
var_dump( get_gps_coordinates_of_street_address( $address ) );
我正在 CodeIgniter 中开发一个项目,我需要特定位置的 lat
和 lng
。底部的代码工作正常,但现在 $geo['status']
显示 QUERY_OVER_LIMIT
。如何解决?
我还收到以下警告:
undefined offset:0 error in $latlng['lat']= $geo['results'][0]['geometry']['location']['lat']; $latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];
提前致谢。这是我的代码:
$address = $this->input->post('key');
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geoco/json?address='.urlencode($address).'&sensor=false');
$geo = json_decode($geo, true);
if ($geo['status'] = 'OK') {
$latlng['lat']= $geo['results'][0]['geometry']['location']['lat'];
$latlng['lng']= $geo['results'][0]['geometry']['location']['lat'];
echo json_encode($latlng);
} else {
echo 0;
}
首先,您对 $geo['status'] = 'OK'
的检查将始终是 'OK',因为您使用了一个“=”而不是“==”。
这意味着即使 google 响应与您预期的不同,您仍然继续进入该代码块。
<?php
$row = [
'address' => '1315 10th St.',
'city' => 'Sacramento',
'state' => 'CA',
'zip' => '95814'
];
$address = str_replace(' ', '+', $row['address'] ) . ','
. str_replace(' ', '+', $row['city'] ) . ','
. str_replace(' ', '+', $row['state'] ) . ','
. str_replace(' ', '+', $row['zip'] );
function get_gps_coordinates_of_street_address( $address )
{
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address='
. $address;
if( $response = @file_get_contents( $url ) )
{
$geo = json_decode( $response, TRUE );
if( $geo['status'] == 'OK' )
{
return $geo['results'][0]['geometry']['location']['lat'] . ',' . $arr['results'][0]['geometry']['location']['lng'];
}
else
{
return 'GPS coordinates were not available for supplied ADDRESS';
}
}
else
{
return 'GPS coordinates were not available because connection failed or malformed request';
}
}
var_dump( get_gps_coordinates_of_street_address( $address ) );