【问题标题】:Can I use Google API for finding nearest cities?我可以使用 Google API 查找最近的城市吗?
【发布时间】:2012-04-27 15:53:56
【问题描述】:

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

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

这是什么意思?我可以使用 API 来获取城市吗?有人知道吗?

【问题讨论】:

  • @muthu 是的,我有一个。但我没有使用地理编码 API。
  • 你知道如何找到城市吗?
  • @muthu 我有一张桌子,上面有城市名称、每个城市的纬度和经度。我正在使用数学公式来计算两个城市之间的距离。但它不会给你准确的距离。

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


【解决方案1】:

在我看来这很清楚:

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

不,如果您不在 Google 地图上显示最近的城市,则不能使用 Geocoding API。

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

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

更新:你可以用GeoNameshere

【讨论】:

  • 感谢安东尼奥的回答。你能给我建议一个替代的想法吗?
  • 问题是如何找到离指定位置最近的城市而不显示在地图上?好吧,我真的不知道,为什么会在地图上显示这样的问题?请更新您的问题或提出一个新问题,也许其他人会知道
【解决方案2】:

我编写了一个代码 sn-p,通过结合 Google Maps Geocoding APIGeoNames.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 的更多信息,请访问以下网址:

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 是的,你是对的。为此,我必须将世界上所有城市的纬度和经度存储在数据库中,然后我必须使用一些公式计算距离。我对吗?但是我从哪里得到那个庞大的数据库呢?你有什么想法吗?
    • 这是数据,不是很大,所有国家的数据都是 216 MB:download.geonames.org/export/dump
    • @AntonioBakula 感谢分享。
    • @AntonioBakula - 嘿,我对你提供的下载有点困惑。我看到所有这些带有国家/地区的文件夹,它们似乎列出了城市,但其中一些重复了几次,例如“Little Mud Creek”在美国列出了两次。解压后的 US.zip 文件本身也有 257mb。
    • 抱歉,不知道,尝试在 geonames.org 寻求帮助或在 SO 上提出其他问题
    猜你喜欢
    • 2019-04-21
    • 2014-07-11
    • 2013-01-27
    • 2022-10-08
    • 1970-01-01
    • 2011-06-04
    • 2023-03-20
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多