原生JS经纬度

效果截图:

JS代码:

function getElem(id) {
	return typeof id === 'string' ? document.getElementById(id) : id;  
}   
function show_it(lat, lon) {  
    var str = '您当前的位置,纬度:' + lat + ',经度:' + lon;  
    getElem('geo_loc').innerHTML = str;  
}
if (navigator.geolocation) {  
    navigator.geolocation.getCurrentPosition(function(position) {
		show_it(position.coords.latitude, position.coords.longitude); 
		}, function(err) {  
            getElem('geo_loc').innerHTML = err.code + "\n" + err.message;});
} else {
		getElem('geo_loc').innerHTML = "您当前使用的浏览器不支持Geolocation服务";   
}  

不知道为啥我的Chrome浏览器用不了。改用Edge可以实现。

猜你喜欢

转载自blog.csdn.net/weixin_40878070/article/details/85921493