HTML5-是什么让这段代码在乡村而不是城市流行?
是什么让下面的代码获取国家而不是城市,我如何更改它以获取城市而不是国家HTML5-是什么让这段代码在乡村而不是城市流行?,html,google-maps,geolocation,latitude-longitude,Html,Google Maps,Geolocation,Latitude Longitude,是什么让下面的代码获取国家而不是城市,我如何更改它以获取城市而不是国家 function get_visitor_country() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position){ var lat = position.coords.latitude; var lon = position
function get_visitor_country() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var country = results[results.length-1].formatted_address;
$("#location-detection .location-name").html(country);
$("#geolocation-worked").css("display", "block");
$("#no-geolocation").css("display", "none");
$geolocation_fix.hide().css({ height : 0 });
init_geolocation_switch();
}
}
});
});
}
}
脚本也在文件末尾加载,如果这可能会影响它。您的脚本当前没有得到任何特别的东西(如城市或国家),它从结果中得到一些东西 要在搜索结果中获取城市搜索,请将类型设置为
[“地区”、“政治”]
当你找到它时,你得到了这个城市
创建标有addressTypes的对象以方便访问:
var components={};
jQuery.each(results,function(i,v){
components[jQuery.camelCase(v.types.join('-'))]=v;
})
//test it
alert((components.localityPolitical)
? components.localityPolitical.formatted_address
: 'n/a');
见: