我正在尝试编写一个Java应用程序,给定一个城市和一个半径,返回所有附近的城市。我认为Google Maps API可以做到这一点,但我对Places API没有好运,它只返回感兴趣的点(例如餐馆......)。有没有办法使用谷歌API,或者你会推荐一些其他API(或这样做的方式)?
答案 0 :(得分:9)
我编写了一个代码段,通过结合 Google Maps Geocoding API 和 GeoNames.org API (除了),您可以检索所有附近的城市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的更多信息,请访问以下网址:
答案 1 :(得分:3)
您可能不希望使用Places API。您可能想看看是否有办法使用地理编码/反向地理编码来获得您想要的内容。请参阅http://code.google.com/apis/maps/documentation/geocoding/。
根据您的效果和准确度要求,您可能无法使用Google Maps API(或任何其他免费工具)轻松完成此操作(尤其是全球范围内),但您可能能够获得某种类型的东西,特别是如果你有地理限制(例如,只是美国),可能会简化事情。