1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 点击百度地图添加标注并获取当前地理名称

点击百度地图添加标注并获取当前地理名称

时间:2023-07-05 04:40:17

相关推荐

点击百度地图添加标注并获取当前地理名称

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>

样式截图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。