【发布时间】:2016-08-10 07:46:46
【问题描述】:
我想使用 php 从给定地址($street、$barangay、$city 和 $province)获取经度和纬度坐标。
【问题讨论】:
-
到目前为止你有什么尝试?
标签: php google-maps geocoding
我想使用 php 从给定地址($street、$barangay、$city 和 $province)获取经度和纬度坐标。
【问题讨论】:
标签: php google-maps geocoding
您可以在此处使用 Google Maps Geocoding API: https://developers.google.com/maps/documentation/geocoding/intro
它是免费的: - 每天 2,500 个免费请求 - 每秒 10 个请求
要使用 Google Geocoding API,请使用此库(MIT 许可): http://geocoder-php.org/
【讨论】:
你可以使用网址:
http://maps.googleapis.com/maps/api/geocode/json?address=YOUR_ADDRESS
它是免费的。
您将获得包含 lat & long 的 json 编码形式的数据
【讨论】:
这是一个 PHP 代码示例,用于根据城镇、城市或国家/地区位置从 Google Maps API 获取纬度和经度值。检查此tutorial 和official documentation。
<?php
$url = "http://maps.google.com/maps/api/geocode/json?address=West+Bridgford&sensor=false®ion=UK";
$response = file_get_contents($url);
$response = json_decode($response, true);
//print_r($response);
$lat = $response['results'][0]['geometry']['location']['lat'];
$long = $response['results'][0]['geometry']['location']['lng'];
echo "latitude: " . $lat . " longitude: " . $long;
?>
http://maps.google.com/maps/api/geocode/json URL 有 3 个参数:地址(您的主要位置)、区域和指示请求是否来自具有位置传感器的设备的传感器。
你也可以查看这个相关的SO question。社区建议使用curl 而不是file_get_contents。
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
【讨论】: