首页 > 代码库 > 定位精准 并打印出来
定位精准 并打印出来
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link href="http://www.mamicode.com/css/mui.min.css" rel="stylesheet" />
<link rel="stylesheet" href="http://www.mamicode.com/css/index.css" />
<script src="http://www.mamicode.com/js/mui.min.js"></script>
<script src="http://api.map.baidu.com/api?v=2.0&ak=Q7hhQzxGQt1Ea0Gg1crZjrSWvtY9eqkR" type="text/javascript"></script>
</head>
<body>
<header class="mui-bar mui-bar-nav" id="header">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">请选择实习位置</h1>
<h4 class=" mui-pull-right">确定</h4>
</header>
<div class="mui-content">
<div id="map" style="height: 40vh;width: 100%;">
</div>
<div class="mui-col-xs-12" id="address">
</div>
</div>
</body>
<script type="text/javascript">
var map = new BMap.Map("map");//创建地图实例
//页面初始化
window.onload=function(){
//初始化地图 默认加载北京天安门
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,18);//初始化地图,point为中心点,缩放级别为16
//判断手机浏览器是否支持定位
if(navigator.geolocation){
var geolocation = new BMap.Geolocation();//创建定位实例
geolocation.getCurrentPosition(showLocation,{enableHighAccuracy: true});//enableHighAccuracy 要求浏览器获取最佳结果
}else{
map.addControl(new BMap.GeolocationControl());//添加定位控件 支持定位
}
};
//处理定位后的信息
function showLocation(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){//定位成功
//新建中心点 并将地图中心移动过去
var geoc=new BMap.Geocoder();
geoc.getLocation(new BMap.Point(r.longitude,r.latitude),function(rs){
var centerPoint = new BMap.Point(r.longitude,r.latitude);
map.panTo(centerPoint);
map.setCenter(centerPoint);
//新建标注
var mk = new BMap.Marker(centerPoint);
mk.disableDragging();// 不可拖拽
map.addOverlay(mk);
for(key in rs.surroundingPois){
console.log(key);
console.log(rs.surroundingPois[key][‘title‘]);
}
console.log(rs.address);
})
}
else {
alert(‘failed‘+this.getStatus());//定位失败
}
}
</script>
</html>
定位精准 并打印出来