使用Google Places API获取指定地点的附近城市

cod*_*odd 11 google-maps-api-3 geocode

我正在尝试编写一个Java应用程序,给定一个城市和一个半径,返回所有附近的城市.我认为Google Maps API可以做到这一点,但我对Places API没有好运,它只返回感兴趣的点(例如餐馆......).有没有办法使用谷歌API,或者你会推荐一些其他API(或这样做的方式)吗?

0x1*_*ad2 9

我编写了一个代码段,允许您通过组合Google Maps Geocoding APIGeoNames.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
}
Run Code Online (Sandbox Code Playgroud)

请注意您的请求金额,因为API是有限的

有关API的更多信息,请访问以下网址:


Tro*_*ott 2

您不想使用 Places API 的说法可能是正确的。您可能想看看是否有一种方法可以使用地理编码/反向地理编码来获得您想要的东西。请参阅http://code.google.com/apis/maps/documentation/geocoding/

根据您的性能和准确性要求,您可能无法使用 Google Maps API(或任何其他免费工具)轻松地做到这一点(尤其是在全球范围内),但您可能可以获得一些基本有效的东西,特别是如果您有地理限制(例如,仅限美国),这可能会简化事情。