Can I use Google API for finding nearest cities?
Asked Answered
B

3

5

I am working on a php project where I want to find and store 3 cities in 100 KM range from a specified location into my database. I was thinking Google API Geocoding for this. I don't whether I'll be able to use it or not. But I found a line in Terms and Conditions page for Geocoding API that

the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.

What does it mean? Can I use the API for fetching the cities? Do anyone have any idea?

Bulwark answered 16/4, 2012 at 10:2 Comment(3)
@muthu yes I got one. But I am not using Geocoding API.Bulwark
Would you know how do you find the cities?Balancer
@Balancer I have one table with city name, latitude and longitude of each city. And I am using a mathematical formula to calculate the distance between the two cities. But it will not give you the accurate distance.Bulwark
D
6

It seems to me that this is very clear:

Yes you can use the Geocoding API if you will show nearest cities on Google map.

No, you can not use the Geocoding API if you will not show nearest cities on Google map.

There is no difference if you will store results and use it later.

btw. note that Yahoo have same usage policy.

Update: You can use GeoNames look here

Dympha answered 16/4, 2012 at 13:22 Comment(2)
Thanks Antonio for your answer. Can you suggest me a alternate idea?Bulwark
And the question is how to find nearest city to specified location without displaying it on map ? Well I really don't know, why is a such a problem display results on a map ? Please update your question or make a new one, maybe someone else will knowDympha
I
3

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:

Involuted answered 3/12, 2015 at 13:18 Comment(0)
G
0

It all boils down to what you want to achieve. Since you still need to use a database to store this location, would it not be better to obtain and store the data (lat and long) of the area your application seeks to cover in the database.

Groundsel answered 16/4, 2012 at 13:14 Comment(4)
Yes you are right. For that I'll have to store all cities of the world in the database with latitude and longitude and then I'll have to calculate the distance using some formula. Am I right? But form where will I get that huge database? Do you have any idea?Bulwark
Here is the data, and it's not huge, data for all countries is 216 MB : download.geonames.org/export/dumpDympha
@AntonioBakula - Hey, I am a bit confused by the download your provided. I see all these folders with countries what seem to list the cities yet it repeats some of them a few times like " Little Mud Creek" is listed in the US one 2 times. Also the US.zip files unzipped is already 257mb by itself.Gravure
sorry, don't know, try to seek some help at geonames.org or ask another question on SODympha

© 2022 - 2024 — McMap. All rights reserved.