html部分:
<divid="allmap"></div>
<styletype="text/css">body,html{width: 100%;height: 100%;overflow:hidden;margin:0;font-family:"微软雅黑";}</style>
js部分:
<scriptdefer type="text/javascript">// 百度地图API功能varx="";vary="";varmap=newBMap.Map("allmap");varpoint=newBMap.Point(116.404, 39.915);map.centerAndZoom(point, 11);varmarker=newBMap.Marker(point); // 创建标注map.addOverlay(marker); // 将标注添加到地图中 setTimeout(function(){map.setZoom(14);}, 1000); //2秒后放大到14级map.enableScrollWheelZoom(true); //缩放地图map.addEventListener("click", showInfo);functionshowInfo(e){x=e.point.lng; //获取鼠标当前点击的经纬度y=e.point.lat;if(x!=""&&y!=""){clearAll(); //清除地图上存在的标注varpoint =newBMap.Point(x,y);map.centerAndZoom(point);marker=newBMap.Marker(point); // 创建新的标注map.addOverlay(marker); //将标注添加到地图上}else{map.centerAndZoom("北京", 12);}
varpoint =newBMap.Point(x,y); //获取当前地理名称vargc =newBMap.Geocoder(); gc.getLocation(point,function(rs){varaddComp = rs.addressComponents;alert(addComp.province+", "+ addComp.city+", "+ addComp.district+", "+ addComp.street +", "+ addComp.streetNumber);});
}functionclearAll(e){map.removeOverlay(marker); }</script>
样式截图: