1

我想找到澳大利亚最近的城市,例如我给出的城市此查看示例。我尝试使用 Google API 但没有用。我怎样才能做到这一点。你可以帮帮我吗?

代码是

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

     var request = {
         location: fenway,
         radius: 500,
         types: ['store']
     };
     var service = new google.maps.places.PlacesService(map);
     service.search(request, callback);

     function callback(results, status) {
         if (status == google.maps.places.PlacesServiceStatus.OK) {
             for (var i = 0; i < results.length; i++) {
                 var lat = results[i].geometry.location.lat();
                 var geocoder = new google.maps.Geocoder();
                 var lng = results[i].geometry.location.lng();
                 var latlng = new google.maps.LatLng(lat, lng);
                 geocoder.geocode({
                     'latLng': latlng
                 }, function (result, status1) {
                     if (status == google.maps.GeocoderStatus.OK) {
                         if (result[1]) {
                             console.log(result[1]);
                         }
                     } else {
                         alert("Geocoder failed due to: " + status1);
                     }
                 });

             }
         }
     }

我想靠近不像商店等的城市。我必须找到澳大利亚的郊区,靠近我给的郊区

4

1 回答 1

2

我编写了一个代码片段,允许您通过结合Google Maps Geocoding APIGeoNames.org API来检索所有附近的城市(除了file_get_contents,您还可以执行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
}

请注意您的请求数量,因为 API 是有限的

有关 API 的更多信息,请访问以下网址:

于 2015-12-03T13:11:50.083 回答