使用Google Places API在特定地点获取附近城市
问题描述:
我正在尝试编写一个Java应用程序,该应用程序给定城市和半径,返回所有附近的城市。我认为Google Maps API能够做到这一点,但我没有使用Places API,只是返回感兴趣的点(例如餐馆,...)。有没有这样做的方式与谷歌API或有一些其他的API(或这样做),你会建议?使用Google Places API在特定地点获取附近城市
答
您很可能不想使用Places API。您可能想查看是否有方法使用地理编码/反向地理编码来获得您想要的内容。见http://code.google.com/apis/maps/documentation/geocoding/。
根据您的性能和准确性要求,您可能无法使用Google Maps API(或任何其他免费工具)轻松做到这一点(特别是全球范围内),但您可能可以获得某种主要类型的东西 - 作品,特别是如果你有地理限制(例如,只是美国),可能会简化事情。
答
我写的代码片段,您可以通过组合谷歌地图API地理编码检索附近的所有城市和GeoNames.org API(除了一个的file_get_contents你也可以做一个卷曲请求)。
/*
* 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的访问欲了解更多信息,以下网址:
如果您觉得这个问题是重复的,请将其标记为这样。不要发布链接到其他堆栈溢出帖子作为答案。 – JAL
好吧,很高兴知道!我想如果我在任何地方发布我的答案,它可能会被标记为重复的答案......我将编辑答案并且不会链接到其他帖子√ – 0x1ad2