开发者

Using the Google Places API to get nearby cities for a given place [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, to开发者_开发问答ols, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

I am trying to write a Java application that, given a city and a radius, returns all the nearby cities. I thought the Google Maps API would be able to do this, but I am getting no luck with the Places API, it just returns points of interest (e.g. restaurants, ...). Is there a way of doing this with the google API or is there some other API (or way of doing this) that you would recommend?


I've written a code snippet that allows you to retrieve all nearby cities by combining the Google Maps Geocoding API and GeoNames.org API (besides a file_get_contents your could also do a cURL request).

/*
 * 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
}

be carefull with your requests amount because the API's are limited

For more information about the API's visit the following url's:

  • https://developers.google.com/maps/documentation/geocoding/intro#GeocodingResponses
  • http://www.geonames.org/export/web-services.html


You are likely correct that you will not want to use the Places API. You may want to see if there is a way to use geocoding/reverse geocoding to get what you want. See http://code.google.com/apis/maps/documentation/geocoding/.

Depending on your performance and accuracy requirements, you may not be able to easily do this (especially globally) with Google Maps API (or any other free tool), but you might be able to get something that sorta kinda mostly-works, especially if you have a geographical restriction (e.g., just the U.S.) that might simplify things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜