我可以使用Google API查找最近的城市吗?

时间:2012-04-16 10:02:25

标签: php google-maps google-geocoding-api

我正在开发一个php项目,我想在100 KM范围内找到并存储3个城市,从指定位置到我的数据库。我正在考虑谷歌API地理编码。我不知道我是否能够使用它。但是我在Geocoding API的条款和条件页面中找到了一行

  

地理编码API只能与Google地图结合使用;禁止在地图上显示地理编码结果。

这是什么意思?我可以使用API​​获取城市吗?有人有任何想法吗?

3 个答案:

答案 0 :(得分:4)

在我看来,这很清楚:

是的,如果您要在Google地图上显示最近的城市,则可以使用地理编码API。

不,如果您不在Google地图上显示最近的城市,则无法使用地理编码API。

如果您将存储结果并在以后使用它,则没有区别。

顺便说一句。请注意,雅虎有相同的使用政策。

更新:您可以使用GeoNames外观here

答案 1 :(得分:2)

我编写了一个代码段,通过结合 Google Maps Geocoding API GeoNames.org API (除了< strong> file_get_contents 您还可以执行 cURL 请求。)

/*
 * Get cities based on city name and radius in KM
 */

// get geocode object as array from The Google Maps Geocoding API
$geocodeObject = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address={CITY NAME},{COUNTRY CODE}'), true);

// get latitude and longitude from geocode object
$latitude = $geocodeObject['results'][0]['geometry']['location']['lat'];
$longitude = $geocodeObject['results'][0]['geometry']['location']['lng'];

// set request options
$responseStyle = 'short'; // the length of the response
$citySize = 'cities15000'; // the minimal number of citizens a city must have
$radius = 30; // the radius in KM
$maxRows = 30; // the maximum number of rows to retrieve
$username = '{YOUR USERNAME}'; // the username of your GeoNames account

// get nearby cities based on range as array from The GeoNames API
$nearbyCities = json_decode(file_get_contents('http://api.geonames.org/findNearbyPlaceNameJSON?lat='.$latitude.'&lng='.$longitude.'&style='.$responseStyle.'&cities='.$citySize.'&radius='.$radius.'&maxRows='.$maxRows.'&username='.$username, true));

// foreach nearby city get city details
foreach($nearbyCities->geonames as $cityDetails)
{
    // do something per nearby city
}
  

请注意您的申请金额,因为API有限

有关API的更多信息,请访问以下网址:

答案 2 :(得分:0)

这一切都归结为你想要实现的目标。由于您仍然需要使用数据库来存储此位置,因此获取和存储应用程序试图在数据库中覆盖的区域的数据(纬度和长度)会不会更好。

相关问题