百度地图----浏览器定位API
2023-09-14 09:01:36 时间
浏览器定位API Geolocation
第一步 引入
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
第二步 放地图的div盒子
<div id="l-map"></div>
第三步 js代码
// 百度地图API功能
var point = null;
var map = new BMap.Map("l-map");
map.centerAndZoom(new BMap.Point(116.395645,39.929986), 7);
map.enableScrollWheelZoom(true);
//获取浏览器定位
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(res){
if(res){
//alert(JSON.stringify(res.point));
//将获取到的经纬度赋值全局变量,方便解析为详细地址使用
point = res.point;
//以该定位经纬度为地图中心显示
map.centerAndZoom(new BMap.Point(res.point.lng,res.point.lat),12);
}
});