В настоящее время я пытаюсь получить параметры 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'];
}