Using the Google Places API to get nearby cities for a specific location

I am trying to write a Java application that, given the city and radius, returns all nearby cities . I thought the Google Maps API could do this, but I was out of luck with the Places API, it just returns points of interest (e.g. restaurants, ...). Is there a way to do this using the Google APIs, or is there any other API (or way to do this) that you would recommend?

+7
source share
2 answers

Most likely, you are correct that you do not want to use the Places API. You can 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 won’t be able to easily do this (especially globally) using the Google Maps API (or any other free tool), but you can get something that you like mostly - it works, especially if you have a geographical restriction (for example, only the United States), which may simplify the situation.

+2
source

I wrote a code snippet that allows you to get all nearby cities by combining the Google Maps geocoding API and the GeoNames.org API (in addition to file_get_contents , you can also execute the 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 careful with the number of your requests, because the API is limited

For more information about the API, visit the following URLs:

+3
source

All Articles