Я знаю, что это возможно в Javascript, но мне нужно сделать это, используя только PHP, как я могу?
Есть ли способ получить long/lat для заданного адреса в Картах Google только с PHP?
Ответ 1
Да, есть интерфейс геокодирования HTTP, который вы можете вызывать с любого языка сервера.
Ответ 2
Иначе:
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
Ответ 3
Используя Google Map API и PHP для получения координат:
$url = 'http://maps.google.com/maps/geo?q='.$address.'&output=json&oe=utf8&sensor=false&key='.$api_key;
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata )&& $jsondata ['Status']['code']==200){
$lat = $jsondata ['Placemark'][0]['Point']['coordinates'][0];
$lon = $jsondata ['Placemark'][0]['Point']['coordinates'][1];
}
Ответ 4
вот мой класс:
class GeoCoder {
public static function getLatLng($address){
try{
$address = urlencode($address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;
if(is_numeric($lat) && is_numeric($lng)){
return array("lat" => $lat, "lng" => $lng);
}
}
catch(Exception $e){
return false;
}
}
}
Ответ 5
Вы можете использовать API Карт Google, чтобы получить адрес с lat и long (пример использования).