How to get names of cities that are around me with API on a remote server?

2

I currently use the lines of code below to list the city where I am through latitude and longitude, but with the same coordinates, I want you to also list the cities that are around, for example mileage. It's possible? I researched a lot about this but I did not find a solution.

Here is the code I'm using to pull the current city:

 $latitude = htmlspecialchars($_GET["latitude"]);
       $longitude = htmlspecialchars($_GET["longitude"]); 
       $geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$longitude.'&sensor=false');
        $resultado = json_decode($geocode);


          for($j=0;$j<count($resultado->results[0]->address_components);$j++){
               $cn=array($resultado->results[0]->address_components[$j]->types[0]);
           if(in_array("locality", $cn))
           {
            $s = $resultado->results[0]->address_components[$j]->long_name;
           }
            } 

I would appreciate it if you could help me find a solution.

    
asked by anonymous 04.01.2016 / 22:24

1 answer

3

I found a post that combines Google Maps Geocoding API with GeoNames.org API to do what you want, below the .php code

$geocodeObject = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address={CITY NAME},{COUNTRY CODE}'), true);

//Pegar a latitude e longitude
$latitude = $geocodeObject['results'][0]['geometry']['location']['lat'];
$longitude = $geocodeObject['results'][0]['geometry']['location']['lng'];

//Entrar com os parametros
$responseStyle = 'short';
$citySize = 'cities15000'; //Voce ainda pode definir um numero minimo de habitantes por cidades
$radius = 30; // Raio em KM
$maxRows = 30;
$username = '{YOUR USERNAME}'; //Seu USERNAME no GeoNames

$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($nearbyCities->geonames as $cityDetails)
{

}

Source: link

I hope I have helped:)

Att. Jeiferson

    
04.01.2016 / 22:37