復制代碼 代碼如下:
<!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>