案例:
设置覆盖物+标注提示文字:
< script> var map = new BMap. Map ( "map" , { mapType : BMAP_HYBRID_MAP } ) ; var point = new BMap. Point ( 120.55294 , 41.665515 ) ; map. centerAndZoom ( point, 18 ) ; var marker = new BMap. Marker ( point) ; map. addOverlay ( marker) ; var label = new BMap. Label ( "我是文字标注哦" , { offset : new BMap. Size ( 20 , - 20 ) } ) ; marker. setLabel ( label) ; var circle = new BMap. Circle ( new BMap. Point ( 120.55294 , 41.665515 ) , 15 , { fillColor : 'red' , fillOpacity : 0.5 , strokeColor : "blue" , strokeWeight : 2 , strokeOpacity : 0.1 } ) ; map. addOverlay ( circle) ; circle. addEventListener ( "click" , attribute) ; function attribute ( ) { var p = circle. point; window. alert ( "marker的位置是" + p. lng + "," + p. lat) ; } map. setCurrentCity ( "朝阳" ) ; map. enableScrollWheelZoom ( true ) ;
< / script>
设置标注的跳动动画的效果和重新设置标注提示的内容,给标注添加点击事件:
< script> var map = new BMap. Map ( "map" , { mapType : BMAP_HYBRID_MAP } ) ; var point = new BMap. Point ( 120.55294 , 41.665515 ) ; map. centerAndZoom ( point, 18 ) ; var marker = new BMap. Marker ( point) ; map. addOverlay ( marker) ; var label = new BMap. Label ( "我是文字标注哦" , { offset : new BMap. Size ( - 18 , - 20 ) } ) ; marker. setLabel ( label) ; marker. setAnimation ( BMAP_ANIMATION_BOUNCE ) ; marker. addEventListener ( "click" , function ( ) { alert ( "你点击了标记!" ) ;
} ) ; function ssa ( ) { label. setContent ( "新的文字标注内容" ) ; alert ( "sjdfk" ) ;
marker. setAnimation ( null ) ; } setTimeout ( ssa, 5000 ) var circle = new BMap. Circle ( new BMap. Point ( 120.55294 , 41.665515 ) , 15 , { fillColor : 'green' , strokeColor : "blue" , fillOpacity : 0.3 , strokeWeight : 2 , strokeOpacity : 0.3 } ) ; map. addOverlay ( circle) ; circle. addEventListener ( "click" , attribute) ; function attribute ( ) { var p = circle. point; window. alert ( "marker的位置是" + p. lng + "," + p. lat) ; } map. setCurrentCity ( "朝阳" ) ; map. enableScrollWheelZoom ( true ) ;
< / script>
增加线
var polyline = new BMap. Polyline ( [ new BMap. Point ( 120.550449 , 41.666586 ) , new BMap. Point ( 120.550207 , 41.665832 ) , new BMap. Point ( 120.552304 , 41.665633 ) , new BMap. Point ( 120.553971 , 41.665444 ) , new BMap. Point ( 120.554034 , 41.665609 ) , new BMap. Point ( 120.555729 , 41.665471 ) , new BMap. Point ( 120.556601 , 41.667068 ) , new BMap. Point ( 120.552388 , 41.66698 ) , new BMap. Point ( 120.552284 , 41.666387 ) , new BMap. Point ( 120.550449 , 41.666586 ) , ] , { strokeColor : "red" , strokeWeight : 2 , strokeOpacity : 0.5 } ) ; map. addOverlay ( polyline) ;
获取坐标信息:
map. addEventListener ( "click" , function ( e ) { layer. alert ( e. point. lng + "<br>" + e. point. lat) ; } ) ;
给标注换图标:
var myIcon = new BMap. Icon ( "./燃脂.png" , new BMap. Size ( 50 , 50 ) ) ; var marker2 = new BMap. Marker ( point, { icon : myIcon } ) ; map. addOverlay ( marker2) ;
开启信息窗口
map. openInfoWindow ( new BMap. InfoWindow ( "天安门坐落在中国北京市中心,故宫的南侧,<br>与天安门广场隔长安街相望,是清朝皇城的大门..." ) , new BMap. Point ( 120.550685 , 41.66632 ) ) ;
设置地图的显示范围,显示层级
var bs = map. getBounds ( ) ; var bssw = bs. getSouthWest ( ) ; var bsne = bs. getNorthEast ( ) ; layer. alert ( "当前地图可视范围是:<br/>" + bssw. lng. toFixed ( 6 ) + ", " + bssw. lat. toFixed ( 6 ) + "<br/>到<br/>" + bsne. lng. toFixed ( 6 ) + ", " + bsne. lat. toFixed ( 6 ) ) ; if ( typeof map != 'undefined' ) { map. addEventListener ( "zoomend" , function ( e ) { layer. msg ( '地图级别: ' + map. getZoom ( ) + ( map. getZoom ( ) < 17 ? ', 示例中只有17级到19地图,超过的无法显示!' : '' ) , { 'offset' : 'b' } ) ; if ( map. getZoom ( ) < 17 ) { map. setZoom ( 18 ) } } ) ; } var b = new BMap. Bounds ( new BMap. Point ( 120.549782 , 41.663656 ) , new BMap. Point ( 120.556098 , 41.667374 ) ) ; try { BMapLib. AreaRestriction. setBounds ( map, b) ; } catch ( e) { alert ( e) ; }