How to find the nearest cities in Google map API

青春壹個敷衍的年華 提交于 2020-01-11 07:37:09

问题


I want to find the nearest cities in the Australia which city i gave for example In this look out the examples. I tried wit h Google API but no use .How can i achieve like this. Could you help me?

code is

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);
                     }
                 });

             }
         }
     }

I want near cities not like the stores etc. I have to find the suburbs in the Australia what are near to the suburb which i given


回答1:


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


来源:https://stackoverflow.com/questions/11649459/how-to-find-the-nearest-cities-in-google-map-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!