如何在Google地图API中查找最近的城市 [英] How to find the nearest cities in Google map API
问题描述
我想找到澳大利亚哪个城市最近的城市,我给了示例在此查看示例。我尝试过使用Google API,但没有用。我该如何实现这一目标。你可以帮我吗?
$ b $ p代码是
var map = new google.maps.Map(document.getElementById(map_canvas),myOptions);
var request = {
位置:fenway,
半径:500,
类型:['store']
};
var service = new google.maps.places.PlacesService(map);
service.search(request,callback);
函数回调(结果,状态){
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);
}
});
$ / code>
我想靠近不喜欢商店的城市等等。我必须在澳大利亚找到郊区。我给出的郊区附近有什么?
我撰写了一段代码段,通过结合 Google地图地理编码API 和 GeoNames.org API (除了 file_get_contents cURL 请求)。
/ **根据城市名称和半径以KM
* /
获取城市//将地理编码对象作为来自Google地图地理编码API的数组获取
$ geocodeObject = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address= {CITY NAME},{COUNTRY CODE}'),true);
//从地理编码对象获取经度和纬度
$ latitude = $ geocodeObject ['results'] [0] ['geometry'] ['location'] ['lat'];
$ longitude = $ geocodeObject ['results'] [0] ['geometry'] ['location'] ['lng'];
//设置请求选项
$ responseStyle ='short'; //响应的长度
$ citySize ='cities15000'; //城市必须有
$ radius = 30的公民的最小数量; //半径KM
$ maxRows = 30; //获取的最大行数
$ username ='{YOUR USERNAME}'; // GeoNames帐户的用户名
//根据GeoNames API中的数组获取邻近城市$ b $ nearbyCities = json_decode(file_get_contents('http://api.geonames。 ?组织/ findNearbyPlaceNameJSON LAT = $纬度。 '和; LNG =' $经度。 '和;风格=' $ responseStyle。 '和;城市=' $ citySize。 '和;半径=' $半径'& maxRows ='。$ maxRows。'& username ='。$ username,true));
//在附近城市获取城市详细信息
foreach($ nearbyCities-> geonames as $ cityDetails)
{
//在附近城市做些事
$ / code>
请谨慎处理您的请求数量,因为API的数量有限
有关API访问的更多信息,请访问以下网址: b $ b
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
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
这篇关于如何在Google地图API中查找最近的城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!