Google maps 如何在谷歌地图API中查找最近的城市
我想找出澳大利亚最近的城市,我在这个例子中给出了哪个城市。我试着用谷歌API,但没有用。我怎么能做到这样呢。你能帮我吗 代码是Google maps 如何在谷歌地图API中查找最近的城市,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我想找出澳大利亚最近的城市,我在这个例子中给出了哪个城市。我试着用谷歌API,但没有用。我怎么能做到这样呢。你能帮我吗 代码是 var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var request = { location: fenway, radius: 500, types: ['store'] };
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);
}
});
}
}
}
var-map=new google.maps.map(document.getElementById(“map\u canvas”),myOptions;
var请求={
地点:芬威,
半径:500,
类型:['store']
};
var service=newgoogle.maps.places.PlacesService(地图);
服务搜索(请求、回调);
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i
我想要的是附近的城市,而不是商店等。我必须找到澳大利亚的郊区,那里离我给出的郊区很近我已经编写了一个代码片段,允许您通过组合谷歌地图地理编码API和GeoNames.org API来检索所有附近的城市(除了文件\u获取内容之外,您还可以执行卷曲请求)
/*
*根据城市名称和以公里为单位的半径获取城市
*/
//从谷歌地图地理编码API获取地理编码对象作为数组
$geocodeObject=json\u解码(文件\u获取\u内容('https://maps.googleapis.com/maps/api/geocode/json?address={城市名称},{国家代码}',true);
//从地理编码对象获取纬度和经度
$latitude=$geocodeObject['results'][0]['geometry']['location']['lat'];
$longitude=$geocodeObject['results'][0]['geometry']['location']['lng'];
//设置请求选项
$responseStyle='short';//响应的长度
$citySize='cities15000';//一个城市必须拥有的最小公民人数
$radius=30;//以公里为单位的半径
$maxRows=30;//要检索的最大行数
$username='{YOUR username}';//您的GeoNames帐户的用户名
//根据GeoNames API中的范围作为数组获取附近的城市
$nearbyCities=json\u decode(文件\u获取\u内容('http://api.geonames.org/findNearbyPlaceNameJSON?lat=“.$latitude.&lng=”.$latitude.&style=”.$responseStyle.&cities=”.$citySize.&radius=”.$radius.&maxRows=”.$maxRows.&username=”.$username,true));
//foreach邻近城市获取城市详细信息
foreach($nearbyCities->geonamesas$cityDetails)
{
//在附近城市做点什么
}
请小心您的请求量,因为API是有限的
有关API的更多信息,请访问以下url:
/*
*根据城市名称和以公里为单位的半径获取城市
*/
//从谷歌地图地理编码API获取地理编码对象作为数组
$geocodeObject=json\u解码(文件\u获取\u内容('https://maps.googleapis.com/maps/api/geocode/json?address={城市名称},{国家代码}',true);
//从地理编码对象获取纬度和经度
$latitude=$geocodeObject['results'][0]['geometry']['location']['lat'];
$longitude=$geocodeObject['results'][0]['geometry']['location']['lng'];
//设置请求选项
$responseStyle='short';//响应的长度
$citySize='cities15000';//一个城市必须拥有的最小公民人数
$radius=30;//以公里为单位的半径
$maxRows=30;//要检索的最大行数
$username='{YOUR username}';//您的GeoNames帐户的用户名
//根据GeoNames API中的范围作为数组获取附近的城市
$nearbyCities=json\u decode(文件\u获取\u内容('http://api.geonames.org/findNearbyPlaceNameJSON?lat=“.$latitude.&lng=”.$latitude.&style=”.$responseStyle.&cities=”.$citySize.&radius=”.$radius.&maxRows=”.$maxRows.&username=”.$username,true));
//foreach邻近城市获取城市详细信息
foreach($nearbyCities->geonamesas$cityDetails)
{
//在附近城市做点什么
}
请小心您的请求量,因为API是有限的
有关API的更多信息,请访问以下url: