在创建Google地图期间获取国家/地区名称以实时更新国家/地区名称字段的最佳方法是什么?
我使用以下代码创建Google地图:
function createMap(address,distance){
address=$("#settingMeetingPrefs_meeting_city").val();
distance=$("#settingMeetingPrefs_meeting_distance").val();
var geocoder = new google.maps.Geocoder();
if (address == null)
var address = "{{city}}";
else
var address = address;
if (distance==null)
distance= "{{distance}}";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
var latlng = new google.maps.LatLng(latitude, longitude);
var myOptions = {
zoom: 7,
center: latlng,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: distance * 1000 ,
// radius: document.getElementById('distance').value * 1000 , // 10 miles in metres
fillColor: '#0000FF'
});
circle.bindTo('center', marker, 'position');
}
});
}
答案 0 :(得分:0)
国家/地区存储在地理编码结果地址组件中。 要获得这个国家,你可以做这样的事情
while (i<results[0].address_components.length){
if(results[0].address_components[i].types[0]=='country'){
console.log(results[0].address_components[i].short_name);
}
i++;
}//end while
此处有更多信息Geocoding Strategies