<!DOCTYPE html>
<html lang="zh">
<head>1
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>百度地图api,js获取位置</title>
</head>
<body>
</body>
</html>
<script src="//api.map.baidu.com/api?v=2.0&ak=你的ak"></script>
<div id="allmap"></div>
<script>
var province=city='';//省市
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
lng=r.point.lng;
lat=r.point.lat;
console.log('您的位置:'+r.point.lng+','+r.point.lat);
// 创建地理编码实例
var myGeo = new BMap.Geocoder();
// 根据坐标得到地址描述
myGeo.getLocation(new BMap.Point(r.point.lng, r.point.lat), function(res){
if (res){
console.log(res);
province=res.addressComponents.province;
city=res.addressComponents.city;
}
});
}else {
console.error('baidu-map-failed'+this.getStatus());
}
});
</script>