В момента се опитвам да получа параметрите lat и lng за адрес чрез използването на API на Google Geocode в PHP страница.
В момента имам следния код, но по някакъв начин той не работи чрез php, докато копирането на генерирания адрес в Google Chrome изглежда работи.
Може ли някой да види грешката в кода по-долу?
Благодаря предварително!
Том
====================================================
Върнатата грешка е:
( [error_message] => The 'sensor' parameter specified in the request must be set to either 'true' or 'false'. [results] => Array ( ) [status] => REQUEST_DENIED ) An error has occured: 1
Стар код с остарели части:
$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);
// retrieve the latitude & longitude from the address
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';
$url = htmlspecialchars($url);
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
if ($response['status'] != 'OK') {
echo 'An error has occured: ' . print_r($response);
} else {
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lat'];
$latitude = $geometry['location']['lng'];
}
====================================================
РЕДАКТИРАНЕ 1 - Добавен е HTML код към всяка уеб страница, за да работи
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
РЕДАКТИРАНЕ 1 - Фиксиран и работещ код:
// create address string
$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];
// retrieve the latitude & longitude from the address
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
if ($response['status'] != 'OK') {
echo 'An error has occured: ' . print_r($response);
} else {
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lat'];
$latitude = $geometry['location']['lng'];
}