Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Google API for finding nearest cities?

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?

like image 821
Subhra Avatar asked Apr 16 '12 10:04

Subhra


People also ask

How do I get a city with Google API?

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.

Is Google nearby API free?

Nearby Search requests return a subset of the supported data fields. 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).

What can you do with Google places API?

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 Answers

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

like image 61
Antonio Bakula Avatar answered Nov 15 '22 03:11

Antonio Bakula


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
like image 20
0x1ad2 Avatar answered Nov 15 '22 04:11

0x1ad2