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?
Once you've made an account and are on the Google Cloud platform, you'll want to go to use the drop down navigation in the top left and choose API & Services > Credentials. Once there, you'll want to hit Create Credentials on the top followed by API key.
The Places API lets you search for place information using a variety of categories, including establishments, prominent points of interest, and geographic locations. You can search for places either by proximity or a text string.
2) Make another web-service call to https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1. This will return a JSON which contains address_components . Looping through the types to find locality and postal_code can give you the city name and postal code.
You are charged for the Nearby Search request starting at 0.032 USD per each, as well as all of the data-type SKUs (Basic Data, Contact Data, and Atmosphere Data).
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With