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

2

12

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?

Lorenelorens answered 26/8, 2011 at 8:55 Comment(0)
G
2

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.

Geraud answered 26/8, 2011 at 18:32 Comment(0)
P
15

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:

Precursor answered 3/12, 2015 at 13:19 Comment(3)
If you feel that this question is a duplicate, please flag it as such. Don't post links to other Stack Overflow posts as an answer.Stigmatism
Alright, good to know! I thought if I post my answer everywhere it's maybe marked as a duplicate answer...I'll edit the answer and don't link to another post √Precursor
geonames rocks !! thanks for the tip !!!!Manageable
G
2

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.

Geraud answered 26/8, 2011 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.