【问题标题】:How to get a list of cities surrounding a city of interest (surrounding suburbs) on google maps api如何在google maps api上获取感兴趣的城市(周边郊区)周围的城市列表
【发布时间】:2017-08-18 17:00:06
【问题描述】:
我们需要找到并列出所有与感兴趣的城市接壤的城市 - 例如,搜索 Ryde, NSW - 我们正在尝试查找每个与 Ryde, NSW 接壤的城市的列表.
我们尝试了半径搜索,但例如 - 如果我们选择 3 公里的标准半径搜索,那么我们搜索一个宽 10 公里、长 10 公里的城市 - 那么 3 公里的半径甚至不会到达城市边界。
关于查询什么以避免这种情况或获取此列表的方法的任何想法?
【问题讨论】:
标签:
node.js
google-maps
google-maps-api-3
【解决方案1】:
Google 的文档不会处理您的问题。但是,我找到了一种使用 google maps api 和 GeoNames api 的方法。不幸的是,您可能在 GeoNames api 和 Google maps api 中有一个帐户。
您可以在 Stack Overflow 中咨询 this answer,下载 GeoNames 的 complete dump 或咨询 documentation。
这段代码完成了这项工作:
/*
* 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
}
注意, 此答案仅解决 google maps api 和 node.js 的问题。这篇文章可以编辑,或者您可以参与其中以添加您自己的答案。
这篇文章还使用了一个代码女巫,在 address 的 Stack Overflow 的另一个答案中给出。
斯芬克斯科技