如何在谷歌地图api上获取一个感兴趣的城市(周边郊区)周围的城市列表

时间:2017-08-18 17:00:07

标签: node.js google-maps google-maps-api-3

我们需要查找并列出所有触及感兴趣城市边界的城市 - 例如,搜索新南威尔士州莱德市 - 我们正在努力查找触及新南威尔士州赖德边境的每个城市的列表

我们尝试了半径搜索,但是例如 - 如果我们选择3km标准半径搜索,那么我们搜索一个10km宽,10km长的城市 - 那么3km半径甚至不会到达城市边界。

有关查询内容以避免此问题的任何想法,或获取此列表的方法?

1 个答案:

答案 0 :(得分:0)

Google的文档不会对您的问题进行处理。但是,我找到了一种使用谷歌地图api和GeoNames api的方法。不幸的是,您可能在GeoNames api和Google maps api中拥有一个帐户。

您可以在Stack Overflow中咨询this answer,下载complete dump GeoNames或咨询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
}

注意, 此答案不能解决谷歌地图api和node.js的问题。可以编辑此帖子,也可以参与其中以添加自己的答案。

这篇文章也使用了代码女巫在这个address的Stack Overflow中的另一个答案中给出。

Sphynx Tech