2011-08-26 15 views
7

Sto provando a scrivere un'applicazione Java che, data una città e un raggio, restituisce tutte le città vicine . Pensavo che l'API di Google Maps sarebbe stata in grado di farlo, ma non ottengo fortuna con l'API di Places, ma restituisce solo i punti di interesse (ad es. Ristoranti, ...). C'è un modo per farlo con l'API di google o ci sono altre API (o un modo per farlo) che consiglieresti?Utilizzo dell'API di Google Places per ottenere città vicine per un dato luogo

risposta

2

È probabile che tu non voglia utilizzare l'API di Places. Potresti voler vedere se c'è un modo per usare geocoding/reverse geocoding per ottenere quello che vuoi. Vedi http://code.google.com/apis/maps/documentation/geocoding/.

A seconda dei requisiti di prestazioni e precisione, potresti non essere in grado di farlo facilmente (soprattutto a livello globale) con l'API di Google Maps (o qualsiasi altro strumento gratuito), ma potresti essere in grado di ottenere qualcosa del genere per lo più- funziona, specialmente se si dispone di una restrizione geografica (ad esempio, solo gli Stati Uniti) che potrebbe semplificare le cose.

3

Ho scritto un frammento di codice che consente di recuperare tutte le città vicine, combinando l'API di Google Maps Geocoding e GeoNames.org API (oltre ad una file_get_contents vostro potrebbe anche fare un richiesta cURL).

/* 
* 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 
} 

fate attenzione con le vostre richieste quantità, perché le API sono limitati

Per ulteriori informazioni sulla visita del API seguente url:

+1

Se ritieni che questa domanda sia duplice, contrassegnala come tale. Non pubblicare link ad altri post di Overflow dello stack come risposta. – JAL

+0

OK, buono a sapersi! Ho pensato che se avessi postato la mia risposta ovunque fosse forse contrassegnata come una risposta duplicata ... Modificherò la risposta e non collegherò a un altro post √ – 0x1ad2

Problemi correlati