javascript使用百度地图api和html5特性获取浏览器位置
人气:0复制代码 代码如下:
<!DOCTYPE html>
<html>
<body>
<p id="demo">点击这个按钮,获得您的位置:</p>
<button onclick="getLocation()">试一下</button>
<script src="http://api.map.baidu.com/api?v=1.4" type="text/javascript"></script>
<script>
var x=document.getElementById("demo");
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}else{
alert("您的浏览器不支持地理定位");
}
}
function showPosition(position){
lat=position.coords.latitude;
lon=position.coords.longitude;
//var map = new BMap.Map("container"); // 创建Map实例
var point = new BMap.Point(lon, lat); // 创建点坐标
//map.centerAndZoom(point,15); //
//map.enableScrollWheelZoom();
var gc = new BMap.Geocoder();
gc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street);
});
}
</script>
</body>
</html>
加载全部内容