风云冷雨吧 关注:2贴子:242

回复:百度地图JavaScript API

只看楼主收藏回复

二、地图控件示例
1、添加/删除工具条、比例尺控件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
#allmap{width:100%;height:500px;}
#r-result{width:100%;margin-top:5px;}
p{margin:5px; font-size:14px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
<title>添加/删除工具条、比例尺控件</title>
</head>
<body>
<div id="allmap"></div>
<div id="r-result">
<input type="button" onclick="add_control();" value="添加" />
<input type="button" onclick="delete_control();" value="删除" />
</div>
<p>在地图的左上、右上分别展示完整、缩略样式的缩放平移控件;同时在地图的左上方展示比例尺控件,点击按钮查看效果</p>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角,添加比例尺
var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角,仅包含平移和缩放按钮
/*缩放控件type有四种类型:
BMAP_NAVIGATION_CONTROL_SMALL:仅包含平移和缩放按钮;BMAP_NAVIGATION_CONTROL_PAN:仅包含平移按钮;BMAP_NAVIGATION_CONTROL_ZOOM:仅包含缩放按钮*/
//添加控件和比例尺
function add_control(){
map.addControl(top_left_control);
map.addControl(top_left_navigation);
map.addControl(top_right_navigation);
}
//移除控件和比例尺
function delete_control(){
map.removeControl(top_left_control);
map.removeControl(top_left_navigation);
map.removeControl(top_right_navigation);
}
2、添加/删除地图类型、缩略图控件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
#allmap{width:100%;height:500px;}
#r-result{width:100%;margin-top:5px;}
p{margin:5px; font-size:14px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
<title>添加/删除地图类型、缩略图控件</title>
</head>
<body>
<div id="allmap"></div>
<div id="r-result">
<input type="button" onclick="add_control();" value="添加" />
<input type="button" onclick="delete_control();" value="删除" />
</div>
<p>点击地图类型控件切换普通地图、卫星图、三维图、混合图(卫星图+路网),右下角是缩略图,点击按钮查看效果</p>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
var mapType1 = new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]});//2D图,卫星图
var mapType2 = new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_LEFT});//左上角,默认地图控件
var overView = new BMap.OverviewMapControl();//默认缩略地图控件
var overViewOpen = new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT}); //将默认缩略地图控件打开放在右下角
//添加地图类型和缩略图
function add_control(){
map.addControl(mapType1); //2D图,卫星图
map.addControl(mapType2); //左上角,默认地图控件
map.setCurrentCity("北京"); //由于有3D图,因此需要设置城市
map.addControl(overView); //添加默认缩略地图控件
map.addControl(overViewOpen); //右下角,打开
}
//移除地图类型和缩略图
function delete_control(){
map.removeControl(mapType1); //移除2D图,卫星图
map.removeControl(mapType2); //移除默认地图控件
map.removeControl(overView); //移除默认缩略地图控件
map.removeControl(overViewOpen); //移除默认缩略地图控件的右下角打开状态
}
</script>
3、添加定位相关控件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
#allmap{width:100%;height:100%;}
p{margin-left:5px; font-size:14px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
<title>添加定位相关控件</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
// 添加带有定位的导航控件
var navigationControl = new BMap.NavigationControl({
// 靠左上角位置
anchor: BMAP_ANCHOR_TOP_LEFT,
// LARGE类型
type: BMAP_NAVIGATION_CONTROL_LARGE,
// 启用显示定位
enableGeolocation: true
});
map.addControl(navigationControl);
// 添加定位控件
var geolocationControl = new BMap.GeolocationControl();
geolocationControl.addEventListener("locationSuccess", function(e){
// 定位成功事件
var address = '';
address += e.addressComponent.province;
address += e.addressComponent.city;
address += e.addressComponent.district;
address += e.addressComponent.street;
address += e.addressComponent.streetNumber;
alert("当前定位地址为:" + address);
});
geolocationControl.addEventListener("locationError",function(e){
// 定位失败事件
alert(e.message);
});
map.addControl(geolocationControl);
</script>


IP属地:浙江90楼2017-04-28 16:53
回复
    4、添加第三方版权控件
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
    <title>添加版权控件</title>
    </head>
    <body>
    <div id="allmap"></div>
    </body>
    </html>
    <script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
    var cr = new BMap.CopyrightControl({anchor: BMAP_ANCHOR_TOP_RIGHT}); //设置版权控件位置
    map.addControl(cr); //添加版权控件
    var bs = map.getBounds(); //返回地图可视区域
    cr.addCopyright({id: 1, content: "<a href='#' style='font-size:20px;background:yellow'>我是自定义版权控件呀</a>", bounds: bs});
    //Copyright(id,content,bounds)类作为CopyrightControl.addCopyright()方法的参数
    </script>
    5、添加自定义控件
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
    #allmap{width:100%;height:500px;}
    p{margin-left:5px; font-size:14px;}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
    <title>添加自定义控件</title>
    </head>
    <body>
    <div id="allmap"></div>
    <p>在地图左上角添加"放大2级"自定义控件,双击放大地图2级</p>
    </body>
    </html>
    <script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
    // 定义一个控件类,即function
    function ZoomControl(){
    // 默认停靠位置和偏移量
    this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
    this.defaultOffset = new BMap.Size(10, 10);
    }
    // 自定义控件必须通过JavaScript的prototype属性继承于BMap.Control
    ZoomControl.prototype = new BMap.Control();
    // 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
    // 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
    ZoomControl.prototype.initialize = function(map){
    // 创建一个DOM元素
    var div = document.createElement("div");
    // 添加文字说明
    div.appendChild(document.createTextNode("放大2级"));
    // 设置样式
    div.style.cursor = "pointer";
    div.style.border = "1px solid gray";
    div.style.backgroundColor = "white";
    // 绑定事件,点击一次放大两级
    div.onclick = function(e){
    map.setZoom(map.getZoom() + 2);
    }
    // 添加DOM元素到地图中
    map.getContainer().appendChild(div);
    // 将DOM元素返回
    return div;
    }
    // 创建控件
    var myZoomCtrl = new ZoomControl();
    // 添加到地图当中
    map.addControl(myZoomCtrl);
    </script>
    6、添加城市列表控件
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
    <title>城市列表控件</title>
    </head>
    <body>
    <div id="allmap"></div>
    </body>
    </html>
    <script type="text/javascript">
    var mp = new BMap.Map("allmap");
    var point = new BMap.Point(116.404, 39.915);
    mp.centerAndZoom(point, 14);
    mp.enableScrollWheelZoom();
    mp.enableInertialDragging();
    mp.enableContinuousZoom();
    var size = new BMap.Size(10, 20);
    mp.addControl(new BMap.CityListControl({
    anchor: BMAP_ANCHOR_TOP_LEFT,
    offset: size,
    // 切换城市之间事件
    // onChangeBefore: function(){
    // alert('before');
    // },
    // 切换城市之后事件
    // onChangeAfter:function(){
    // alert('after');
    // }
    }));
    </script>


    IP属地:浙江91楼2017-04-28 16:56
    回复
      三、覆盖物示例
      1、添加/删除覆盖物
      <!DOCTYPE html>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
      <style type="text/css">
      body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
      #allmap{height:500px;width:100%;}
      #r-result{width:100%;}
      </style>
      <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
      <title>添加/删除覆盖物</title>
      </head>
      <body>
      <div id="allmap"></div>
      <div id="r-result">
      <input type="button" onclick="add_overlay();" value="添加覆盖物" />
      <input type="button" onclick="remove_overlay();" value="删除覆盖物" />
      </div>
      </body>
      </html>
      <script type="text/javascript">
      // 百度地图API功能
      var map = new BMap.Map("allmap");
      var point = new BMap.Point(116.404, 39.915);
      map.centerAndZoom(point, 15);
      var marker = new BMap.Marker(new BMap.Point(116.404, 39.915)); // 创建点
      var polyline = new BMap.Polyline([
      new BMap.Point(116.399, 39.910),
      new BMap.Point(116.405, 39.920),
      new BMap.Point(116.425, 39.900)
      ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //创建折线
      var circle = new BMap.Circle(point,500,{strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //创建圆
      var polygon = new BMap.Polygon([
      new BMap.Point(116.387112,39.920977),
      new BMap.Point(116.385243,39.913063),
      new BMap.Point(116.394226,39.917988),
      new BMap.Point(116.401772,39.921364),
      new BMap.Point(116.41248,39.927893)
      ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //创建多边形
      var pStart = new BMap.Point(116.392214,39.918985);
      var pEnd = new BMap.Point(116.41478,39.911901);
      var rectangle = new BMap.Polygon([
      new BMap.Point(pStart.lng,pStart.lat),
      new BMap.Point(pEnd.lng,pStart.lat),
      new BMap.Point(pEnd.lng,pEnd.lat),
      new BMap.Point(pStart.lng,pEnd.lat)
      ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //创建矩形
      //添加覆盖物
      function add_overlay(){
      map.addOverlay(marker); //增加点
      map.addOverlay(polyline); //增加折线
      map.addOverlay(circle); //增加圆
      map.addOverlay(polygon); //增加多边形
      map.addOverlay(rectangle); //增加矩形
      }
      //清除覆盖物
      function remove_overlay(){
      map.clearOverlays();
      }
      </script>
      2、设置点的弹跳动画
      <!DOCTYPE html>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
      <style type="text/css">
      body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
      </style>
      <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
      <title>添加动画标注点</title>
      </head>
      <body>
      <div id="allmap"></div>
      </body>
      </html>
      <script type="text/javascript">
      // 百度地图API功能
      var map = new BMap.Map("allmap");
      var point = new BMap.Point(116.404, 39.915);
      map.centerAndZoom(point, 15);
      var marker = new BMap.Marker(point); // 创建标注
      map.addOverlay(marker); // 将标注添加到地图中
      marker.setA***tion(BMAP_ANIMATION_BOUNCE); //跳动的动画
      </script>


      IP属地:浙江92楼2017-04-29 14:59
      回复
        3、设置点的新图标
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
        </style>
        <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
        <title>设置点的新图标</title>
        </head>
        <body>
        <div id="allmap"></div>
        </body>
        </html>
        <script type="text/javascript">
        // 百度地图API功能
        var map = new BMap.Map("allmap");
        var point = new BMap.Point(116.404, 39.915);
        map.centerAndZoom(point, 15);
        //创建小狐狸
        var pt = new BMap.Point(116.417, 39.909);
        var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdemo/img/fox.bmp", new BMap.Size(300,157));
        var marker2 = new BMap.Marker(pt,{icon:myIcon}); // 创建标注
        map.addOverlay(marker2); // 将标注添加到地图中
        </script>


        IP属地:浙江94楼2017-04-29 15:50
        回复
          4、设置点是否可拖拽
          <!DOCTYPE html>
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
          <style type="text/css">
          body, html,#allmap {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
          #l-map{height:500px;width:100%;}
          #r-result{width:100%;}
          </style>
          <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
          <title>设置点是否可拖拽</title>
          </head>
          <body>
          <div id="l-map"></div>
          <div id="r-result">
          <input type="button" onclick="marker.enableDragging();" value="可拖拽" />
          <input type="button" onclick="marker.disableDragging();" value="不可拖拽" />
          </div>
          </body>
          </html>
          <script type="text/javascript">
          // 百度地图API功能
          var map = new BMap.Map("l-map");
          var point = new BMap.Point(116.400244,39.92556);
          map.centerAndZoom(point, 12);
          var marker = new BMap.Marker(point);// 创建标注
          map.addOverlay(marker); // 将标注添加到地图中
          marker.disableDragging(); // 不可拖拽
          </script>
          5、设置点、面可编辑
          <!DOCTYPE html>
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
          <style type="text/css">
          body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
          #allmap {height:500px; width: 100%;}
          #control{width:100%;}
          </style>
          <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
          <title>设置线、面可编辑</title>
          </head>
          <body>
          <div id="allmap"></div>
          <div id="control">
          <button onclick = "polyline.enableEditing();polygon.enableEditing();">开启线、面编辑功能</button>
          <button onclick = "polyline.disableEditing();polygon.disableEditing();">关闭线、面编辑功能</button>
          </div>
          </body>
          </html>
          <script type="text/javascript">
          // 百度地图API功能
          var map = new BMap.Map("allmap");
          map.centerAndZoom(new BMap.Point(116.404, 39.915), 15);
          map.enableScrollWheelZoom();
          var polyline = new BMap.Polyline([
          new BMap.Point(116.399, 39.910),
          new BMap.Point(116.405, 39.920),
          new BMap.Point(116.423493, 39.907445)
          ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //创建折线
          map.addOverlay(polyline); //增加折线
          var polygon = new BMap.Polygon([
          new BMap.Point(116.387112,39.920977),
          new BMap.Point(116.385243,39.913063),
          new BMap.Point(116.394226,39.917988),
          new BMap.Point(116.401772,39.921364),
          new BMap.Point(116.41248,39.927893)
          ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); //创建多边形
          map.addOverlay(polygon); //增加多边形
          </script>
          6、设置覆盖物显示/隐藏
          <!DOCTYPE html>
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
          <style type="text/css">
          body, html,#allmap {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
          #l-map{height:500px;width:100%;}
          </style>
          <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
          <title>设置覆盖物的显示与隐藏</title>
          </head>
          <body>
          <div id="l-map"></div>
          <div id="r-result">
          <input type="button" onclick="showOver()" value="显示" />
          <input type="button" onclick="hideOver()" value="隐藏" />
          </div>
          </body>
          </html>
          <script type="text/javascript">
          // 百度地图API功能
          var map = new BMap.Map("l-map");
          var point = new BMap.Point(116.404, 39.915);
          map.centerAndZoom(point, 15);
          var marker = new BMap.Marker(new BMap.Point(116.404, 39.915)); // 创建标注
          map.addOverlay(marker); // 将标注添加到地图中
          var polyline = new BMap.Polyline([
          new BMap.Point(116.399, 39.910),
          new BMap.Point(116.405, 39.920),
          new BMap.Point(116.425, 39.900)
          ], {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
          map.addOverlay(polyline); //增加折线
          var circle = new BMap.Circle(point,500);
          map.addOverlay(circle); //增加圆
          hideOver();
          function showOver(){
          marker.show(); polyline.show(); circle.show();
          }
          function hideOver(){
          marker.hide(); polyline.hide(); circle.hide();
          }
          </script>


          IP属地:浙江95楼2017-04-29 16:03
          回复
            7、添加文字标签
            <!DOCTYPE html>
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
            <style type="text/css">
            body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
            </style>
            <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
            <title>文本标注</title>
            </head>
            <body>
            <div id="allmap"></div>
            </body>
            </html>
            <script type="text/javascript">
            // 百度地图API功能
            var map = new BMap.Map("allmap");
            var point = new BMap.Point(116.417854,39.921988);
            map.centerAndZoom(point, 15);
            var opts = {
            position : point, // 指定文本标注所在的地理位置
            offset : new BMap.Size(30, -30) //设置文本偏移量
            }
            var label = new BMap.Label("欢迎使用百度地图,这是一个简单的文本标注哦~", opts); // 创建文本标注对象
            label.setStyle({
            color : "red",
            fontSize : "12px",
            height : "20px",
            lineHeight : "20px",
            fontFamily:"微软雅黑"
            });
            map.addOverlay(label);
            </script>
            8、设置覆盖物的文字标签
            <!DOCTYPE html>
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
            <style type="text/css">
            body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
            </style>
            <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
            <title>带文字标签的覆盖物</title>
            </head>
            <body>
            <div id="allmap"></div>
            </body>
            </html>
            <script type="text/javascript">
            // 百度地图API功能
            var map = new BMap.Map("allmap");
            var point = new BMap.Point(116.400244,39.92556);
            map.centerAndZoom(point, 12);
            var marker = new BMap.Marker(point); // 创建标注
            map.addOverlay(marker); // 将标注添加到地图中
            var label = new BMap.Label("我是文字标注哦",{offset:new BMap.Size(20,-10)});
            marker.setLabel(label);
            </script>
            9、获取覆盖物信息
            <!DOCTYPE html>
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
            <style type="text/css">
            body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
            </style>
            <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
            <title>获取覆盖物的信息</title>
            </head>
            <body>
            <div id="allmap"></div>
            </body>
            </html>
            <script type="text/javascript">
            // 百度地图API功能
            var map = new BMap.Map("allmap");
            var point = new BMap.Point(116.400244,39.92556);
            map.centerAndZoom(point, 12);
            var marker = new BMap.Marker(point); // 创建标注
            map.addOverlay(marker); // 将标注添加到地图中
            marker.addEventListener("click",getAttr);
            function getAttr(){
            var p = marker.getPosition(); //获取marker的位置
            alert("marker的位置是" + p.lng + "," + p.lat);
            }
            </script>


            IP属地:浙江96楼2017-04-29 16:31
            回复
              10、添加多个点示例
              <!DOCTYPE html>
              <html>
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
              <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
              <style type="text/css">
              body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
              #l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
              #r-result{height:100%;width:20%;float:left;}
              </style>
              <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
              <title>添加多个标注点</title>
              </head>
              <body>
              <div id="allmap"></div>
              </body>
              </html>
              <script type="text/javascript">
              // 百度地图API功能
              var map = new BMap.Map("allmap");
              var point = new BMap.Point(116.404, 39.915);
              map.centerAndZoom(point, 15);
              // 编写自定义函数,创建标注
              function addMarker(point){
              var marker = new BMap.Marker(point);
              map.addOverlay(marker);
              }
              // 随机向地图添加25个标注
              var bounds = map.getBounds();
              var sw = bounds.getSouthWest();
              var ne = bounds.getNorthEast();
              var lngSpan = Math.abs(sw.lng - ne.lng);
              var latSpan = Math.abs(ne.lat - sw.lat);
              for (var i = 0; i < 25; i ++) {
              var point = new BMap.Point(sw.lng + lngSpan * (Math.random() * 0.7), ne.lat - latSpan * (Math.random() * 0.7));
              addMarker(point);
              }
              </script>
              11、从多个点里删除特定点
              <!DOCTYPE html>
              <html>
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
              <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
              <style type="text/css">
              body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
              #allmap{width:100%;height:500px;}
              </style>
              <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
              <title>从多个点删除指定点</title>
              </head>
              <body>
              <div id="allmap"></div>
              <input type="button" onclick="deletePoint()" value="删除id=1"/>
              </body>
              </html>
              <script type="text/javascript">
              // 百度地图API功能
              var map = new BMap.Map("allmap");
              var point = new BMap.Point(116.404, 39.915);
              map.centerAndZoom(point, 15);
              map.disableDoubleClickZoom(true);
              // 编写自定义函数,创建标注
              function addMarker(point,label){
              var marker = new BMap.Marker(point);
              map.addOverlay(marker);
              marker.setLabel(label);
              }
              // 随机向地图添加25个标注
              var bounds = map.getBounds();
              var sw = bounds.getSouthWest();
              var ne = bounds.getNorthEast();
              var lngSpan = Math.abs(sw.lng - ne.lng);
              var latSpan = Math.abs(ne.lat - sw.lat);
              for (var i = 0; i < 10; i++) {
              var point = new BMap.Point(sw.lng + lngSpan * (Math.random() * 0.7), ne.lat - latSpan * (Math.random() * 0.7));
              var label = new BMap.Label("我是id="+i,{offset:new BMap.Size(20,-10)});
              addMarker(point,label);
              }
              function deletePoint(){
              var allOverlay = map.getOverlays();
              for (var i = 0; i < allOverlay.length -1; i++){
              if(allOverlay[i].getLabel().content == "我是id=1"){
              map.removeOverlay(allOverlay[i]);
              return false;
              }
              }
              }
              </script>


              IP属地:浙江97楼2017-04-29 16:49
              回复
                12、加载闪烁点
                <!DOCTYPE HTML>
                <html>
                <head>
                <title>加载闪烁点</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
                <style type="text/css">
                html,body{
                margin:0;
                width:100%;
                height:100%;
                background:#333;
                }
                #map{
                width:100%;
                height:100%;
                }
                #panel {
                position: absolute;
                top:30px;
                left:10px;
                z-index: 999;
                color: #fff;
                }
                #login{
                position:absolute;
                width:300px;
                height:40px;
                left:50%;
                top:50%;
                margin:-40px 0 0 -150px;
                }
                #login input[type=password]{
                width:200px;
                height:30px;
                padding:3px;
                line-height:30px;
                border:1px solid #000;
                }
                #login input[type=submit]{
                width:80px;
                height:38px;
                display:inline-block;
                line-height:38px;
                }
                </style>
                <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                </head>
                <body>
                <div id="map"></div>
                <script type="text/javascript">
                var map = new BMap.Map("map", {}); // 创建Map实例
                map.centerAndZoom(new BMap.Point(116.404, 39.915), 8); // 初始化地图,设置中心点坐标和地图级别
                map.enableScrollWheelZoom(); //启用滚轮放大缩小
                if (document.createElement('canvas').getContext) {
                var mapStyle ={
                features: ["road", "building","water","land"],//隐藏地图上的poi
                style : "dark" //设置地图风格为高端黑
                }
                map.setMapStyle(mapStyle);
                var BW = 0, //canvas width
                BH = 0, //canvas height
                ctx = null,
                stars = [], //存储所有星星对象的数组
                timer = null, //定时器
                timeLine = null, //时间轴对象
                rs = [], //最新的结果
                isNowTimeData = false, //是否显示当前时间的定位情况
                py = null, //偏移
                gridWidth = 10000,//网格的大小
                isOverlay = false,//是否叠加
                //gridWidth = 1,//网格的大小
                canvas = null; //偏移
                function Star(options){
                this.init(options);
                }
                Star.prototype.init = function(options) {
                this.x = ~~(options.x);
                this.y = ~~(options.y);
                this.initSize(options.size);
                if (~~(0.5 + Math.random() * 7) == 1) {
                this.size = 0;
                } else {
                this.size = this.maxSize;
                }
                }
                Star.prototype.initSize = function(size) {
                var size = ~~(size);
                this.maxSize = size > 6 ? 6 : size;
                }
                Star.prototype.render = function(i) {
                var p = this;
                if(p.x < 0 || p.y <0 || p.x > BW || p.y > BH) {
                return;
                }
                ctx.beginPath();
                var gradient = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.size);
                gradient.addColorStop(0, "rgba(7,120,249,1)");
                gradient.addColorStop(1, "rgba(7,120,249,0.3)");
                ctx.fillStyle = gradient;
                ctx.arc(p.x, p.y, p.size, Math.PI*2, false);
                ctx.fill();
                if (~~(0.5 + Math.random() * 7) == 1) {
                p.size = 0;
                } else {
                p.size = p.maxSize;
                }
                }
                function render(){
                renderAction();
                setTimeout(render, 180);
                }
                function renderAction() {
                ctx.clearRect(0, 0, BW, BH);
                ctx.globalCompositeOperation = "lighter";
                for(var i = 0, len = stars.length; i < len; ++i){
                if (stars[i]) {
                stars[i].render(i);
                }
                }
                }
                // 复杂的自定义覆盖物
                function ComplexCustomOverlay(point){
                this._point = point;
                }
                ComplexCustomOverlay.prototype = new BMap.Overlay();
                ComplexCustomOverlay.prototype.initialize = function(map){
                this._map = map;
                canvas = this.canvas = document.createElement("canvas");
                canvas.style.cssText = "position:absolute;left:0;top:0;";
                ctx = canvas.getContext("2d");
                var size = map.getSize();
                canvas.width = BW = size.width;
                canvas.height = BH = size.height;
                map.getPanes().labelPane.appendChild(canvas);
                //map.getContainer().appendChild(canvas);
                return this.canvas;
                }
                ComplexCustomOverlay.prototype.draw = function(){
                var map = this._map;
                var bounds = map.getBounds();
                var sw = bounds.getSouthWest();
                var ne = bounds.getNorthEast();
                var pixel = map.pointToOverlayPixel(new BMap.Point(sw.lng, ne.lat));
                py = pixel;
                if (rs.length > 0) {
                showStars(rs);
                }
                }
                var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(116.407845,39.914101));
                map.addOverlay(myCompOverlay);
                var project = map.getMapType().getProjection();
                var bounds = map.getBounds();
                var sw = bounds.getSouthWest();
                var ne = bounds.getNorthEast();
                sw = project.lngLatToPoint(new BMap.Point(sw.lng, sw.lat));
                ne = project.lngLatToPoint(new BMap.Point(ne.lng, ne.lat));
                //左上角墨卡托坐标点
                var original = {
                x : sw.x,
                y : ne.y
                }
                /**
                * 请求定位数据,并在地图上绘制出
                * @param 请求的时间
                * @param 成功后执行的回调函数
                *
                */
                var requestMgr = {
                request: function (time, successCbk) {
                if (document.location.host === 'developer.baidu.com') {
                var url = "http://developer.baidu.com/map/jsdemo/data/data";
                } else {
                var url = "../data/data";
                }
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function() {
                if( xhr.readyState == 4 && xhr.status == 200 ) {
                if (!isOverlay) {
                rs = JSON.parse(xhr.responseText);
                } else {
                rs = rs.concat(JSON.parse(xhr.responseText));
                if (rs.length > 10000) {
                rs.splice(0, rs.length - 10000);
                }
                }
                showStars(rs);
                if (successCbk) {
                successCbk();
                }
                }
                }
                xhr.open( "GET", url, true );
                xhr.send( null );
                }
                }
                //显示星星
                function showStars(rs) {
                stars.length = 0;
                var temp = {};
                for (var i = 0, len = rs.length; i < len; i++) {
                var item = rs[i];
                var addNum = gridWidth / 2;
                var x = item[0] * gridWidth + addNum;
                var y = item[1] * gridWidth + addNum;
                var point = project.pointToLngLat(new BMap.Pixel(x, y));
                var px = map.pointToOverlayPixel(point);
                //create all stars
                var s = new Star({
                x: px.x - py.x,
                y: px.y - py.y,
                size: item[2]
                });
                stars.push(s);
                //}
                }
                canvas.style.left = py.x + "px";
                canvas.style.top = py.y + "px";
                renderAction();
                }
                render();
                function nowTimeCbk (time) {
                requestMgr.request(time, function(){
                if (isNowTimeData) {
                setTimeout(function(){
                if (isNowTimeData) {
                startCbk(nowTimeCbk);
                }
                }, 1000);
                }
                });
                }
                function startCbk(cbk){
                var now = new Date();
                var time = {
                hour : now.getHours(),
                minute : now.getMinutes(),
                second : now.getSeconds()
                };
                if (cbk) {
                cbk(time);
                }
                };
                startCbk(nowTimeCbk);
                } else {
                alert('请在chrome、safari、IE8+以上浏览器查看本示例');
                }
                </script>
                </body>
                </html>


                IP属地:浙江98楼2017-04-29 16:49
                收起回复
                  13、加载海量点
                  <!DOCTYPE HTML>
                  <html>
                  <head>
                  <title>加载海量点</title>
                  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
                  <style type="text/css">
                  html,body{
                  margin:0;
                  width:100%;
                  height:100%;
                  background:#ffffff;
                  }
                  #map{
                  width:100%;
                  height:100%;
                  }
                  #panel {
                  position: absolute;
                  top:30px;
                  left:10px;
                  z-index: 999;
                  color: #fff;
                  }
                  #login{
                  position:absolute;
                  width:300px;
                  height:40px;
                  left:50%;
                  top:50%;
                  margin:-40px 0 0 -150px;
                  }
                  #login input[type=password]{
                  width:200px;
                  height:30px;
                  padding:3px;
                  line-height:30px;
                  border:1px solid #000;
                  }
                  #login input[type=submit]{
                  width:80px;
                  height:38px;
                  display:inline-block;
                  line-height:38px;
                  }
                  </style>
                  <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                  <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/data/points-sample-data.js"></script>
                  </head>
                  <body>
                  <div id="map"></div>
                  <script type="text/javascript">
                  var map = new BMap.Map("map", {}); // 创建Map实例
                  map.centerAndZoom(new BMap.Point(105.000, 38.000), 5); // 初始化地图,设置中心点坐标和地图级别
                  map.enableScrollWheelZoom(); //启用滚轮放大缩小
                  if (document.createElement('canvas').getContext) { // 判断当前浏览器是否支持绘制海量点
                  var points = []; // 添加海量点数据
                  for (var i = 0; i < data.data.length; i++) {
                  points.push(new BMap.Point(data.data[i][0], data.data[i][1]));
                  }
                  var options = {
                  size: BMAP_POINT_SIZE_SMALL,
                  shape: BMAP_POINT_SHAPE_STAR,
                  color: '#d340c3'
                  }
                  var pointCollection = new BMap.PointCollection(points, options); // 初始化PointCollection
                  pointCollection.addEventListener('click', function (e) {
                  alert('单击点的坐标为:' + e.point.lng + ',' + e.point.lat); // 监听点击事件
                  });
                  map.addOverlay(pointCollection); // 添加Overlay
                  } else {
                  alert('请在chrome、safari、IE8+以上浏览器查看本示例');
                  }
                  </script>
                  </body>
                  </html>
                  14、添加弧线
                  <!DOCTYPE html>
                  <html>
                  <head>
                  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                  <title>添加弧线</title>
                  <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                  <script type="text/javascript" src="http://api.map.baidu.com/library/CurveLine/1.5/src/CurveLine.min.js"></script>
                  <style type="text/css">
                  html,body{
                  width:100%;
                  height:100%;
                  margin:0;
                  overflow:hidden;
                  font-family:"微软雅黑";
                  }
                  </style>
                  </head>
                  <body>
                  <div style="width:100%;height:100%;border:1px solid gray" id="container"></div>
                  </body>
                  </html>
                  <script type="text/javascript">
                  // 百度地图API功能
                  var map = new BMap.Map("container");
                  map.centerAndZoom(new BMap.Point(118.454, 32.955), 6);
                  map.enableScrollWheelZoom();
                  var beijingPosition=new BMap.Point(116.432045,39.910683),
                  hangzhouPosition=new BMap.Point(120.129721,30.314429),
                  taiwanPosition=new BMap.Point(121.491121,25.127053);
                  var points = [beijingPosition,hangzhouPosition, taiwanPosition];
                  var curve = new BMapLib.CurveLine(points, {strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //创建弧线对象
                  map.addOverlay(curve); //添加到地图中
                  curve.enableEditing(); //开启编辑功能
                  </script>


                  IP属地:浙江99楼2017-04-30 19:56
                  回复
                    15、添加椭圆
                    <!DOCTYPE html>
                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                    <style type="text/css">
                    body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
                    #allmap{width:100%;height:500px;}
                    p{margin-left:5px; font-size:14px;}
                    </style>
                    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                    <script type="text/javascript" src="http://api.map.baidu.com/library/CurveLine/1.5/src/CurveLine.min.js"></script>
                    <title>添加椭圆</title>
                    </head>
                    <body>
                    <div id="allmap"></div>
                    <p>在地图上添加一个椭圆,圆的颜色为蓝色、线宽6px、透明度为1,填充颜色为白色,透明度为0.5</p>
                    </body>
                    </html>
                    <script type="text/javascript">
                    // 百度地图API功能
                    var map = new BMap.Map("allmap");
                    var point = new BMap.Point(116.404, 39.915);
                    map.centerAndZoom(point, 10);
                    //centre:椭圆中心点,X:横向经度,Y:纵向纬度
                    function add_oval(centre,x,y)
                    {
                    var assemble=new Array();
                    var angle;
                    var dot;
                    var tangent=x/y;
                    for(i=0;i<36;i++)
                    {
                    angle = (2* Math.PI / 36) * i;
                    dot = new BMap.Point(centre.lng+Math.sin(angle)*y*tangent, centre.lat+Math.cos(angle)*y);
                    assemble.push(dot);
                    }
                    return assemble;
                    }
                    var oval = new BMap.Polygon(add_oval(point,0.1,0.3), {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
                    map.addOverlay(oval);
                    </script>
                    16、添加行政区划
                    <!DOCTYPE html>
                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                    <style type="text/css">
                    body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
                    </style>
                    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                    <title>添加行政区划</title>
                    </head>
                    <body>
                    <div id="allmap"></div>
                    </body>
                    </html>
                    <script type="text/javascript">
                    // 百度地图API功能
                    var map = new BMap.Map("allmap");
                    map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
                    map.enableScrollWheelZoom();
                    function getBoundary(){
                    var bdary = new BMap.Boundary();
                    bdary.get("北京市海淀区", function(rs){ //获取行政区域
                    map.clearOverlays(); //清除地图覆盖物
                    var count = rs.boundaries.length; //行政区域的点有多少个
                    if (count === 0) {
                    alert('未能获取当前输入行政区域');
                    return ;
                    }
                    var pointArray = [];
                    for (var i = 0; i < count; i++) {
                    var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
                    map.addOverlay(ply); //添加覆盖物
                    pointArray = pointArray.concat(ply.getPath());
                    }
                    map.setViewport(pointArray); //调整视野
                    addlabel();
                    });
                    }
                    setTimeout(function(){
                    getBoundary();
                    }, 2000);
                    function addlabel() {
                    var pointArray = [
                    new BMap.Point(121.716076,23.703799),
                    new BMap.Point(112.121885,14.570616),
                    new BMap.Point(123.776573,25.695422)];
                    var optsArray = [{},{},{}];
                    var labelArray = [];
                    var contentArray = [
                    "台湾是中国的!",
                    "南海是中国的!",
                    "钓鱼岛是中国的!"];
                    for(var i = 0;i < pointArray.length; i++) {
                    optsArray[i].position = pointArray[i];
                    labelArray[i] = new BMap.Label(contentArray[i],optsArray[i]);
                    labelArray[i].setStyle({
                    color : "red",
                    fontSize : "12px",
                    height : "20px",
                    lineHeight : "20px",
                    fontFamily:"微软雅黑"
                    });
                    map.addOverlay(labelArray[i]);
                    }
                    }
                    </script>


                    IP属地:浙江100楼2017-04-30 23:41
                    回复
                      17、添加自定义覆盖物
                      <!DOCTYPE html>
                      <html>
                      <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                      <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                      <style type="text/css">
                      body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
                      #allmap{width:100%;height:500px;}
                      p{margin-left:5px; font-size:14px;}
                      </style>
                      <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                      <title>添加自定义覆盖物</title>
                      </head>
                      <body>
                      <div id="allmap"></div>
                      <p>图示中为房产覆盖物,鼠标移到覆盖物上,自动显示房屋套数</p>
                      </body>
                      </html>
                      <script type="text/javascript">
                      // 百度地图API功能
                      var mp = new BMap.Map("allmap");
                      mp.centerAndZoom(new BMap.Point(116.3964,39.9093), 15);
                      mp.enableScrollWheelZoom();
                      // 复杂的自定义覆盖物
                      function ComplexCustomOverlay(point, text, mouseoverText){
                      this._point = point;
                      this._text = text;
                      this._overText = mouseoverText;
                      }
                      ComplexCustomOverlay.prototype = new BMap.Overlay();
                      ComplexCustomOverlay.prototype.initialize = function(map){
                      this._map = map;
                      var div = this._div = document.createElement("div");
                      div.style.position = "absolute";
                      div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
                      div.style.backgroundColor = "#EE5D5B";
                      div.style.border = "1px solid #BC3B3A";
                      div.style.color = "white";
                      div.style.height = "18px";
                      div.style.padding = "2px";
                      div.style.lineHeight = "18px";
                      div.style.whiteSpace = "nowrap";
                      div.style.MozUserSelect = "none";
                      div.style.fontSize = "12px"
                      var span = this._span = document.createElement("span");
                      div.appendChild(span);
                      span.appendChild(document.createTextNode(this._text));
                      var that = this;
                      var arrow = this._arrow = document.createElement("div");
                      arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label..bmp) no-repeat";
                      arrow.style.position = "absolute";
                      arrow.style.width = "11px";
                      arrow.style.height = "10px";
                      arrow.style.top = "22px";
                      arrow.style.left = "10px";
                      arrow.style.overflow = "hidden";
                      div.appendChild(arrow);
                      div.onmouseover = function(){
                      this.style.backgroundColor = "#6BADCA";
                      this.style.borderColor = "#0000ff";
                      this.getElementsByTagName("span")[0].innerHTML = that._overText;
                      arrow.style.backgroundPosition = "0px -20px";
                      }
                      div.onmouseout = function(){
                      this.style.backgroundColor = "#EE5D5B";
                      this.style.borderColor = "#BC3B3A";
                      this.getElementsByTagName("span")[0].innerHTML = that._text;
                      arrow.style.backgroundPosition = "0px 0px";
                      }
                      mp.getPanes().labelPane.appendChild(div);
                      return div;
                      }
                      ComplexCustomOverlay.prototype.draw = function(){
                      var map = this._map;
                      var pixel = map.pointToOverlayPixel(this._point);
                      this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
                      this._div.style.top = pixel.y - 30 + "px";
                      }
                      var txt = "银湖海岸城", mouseoverTxt = txt + " " + parseInt(Math.random() * 1000,10) + "套" ;
                      var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(116.407845,39.914101), "银湖海岸城",mouseoverTxt);
                      mp.addOverlay(myCompOverlay);
                      </script>


                      IP属地:浙江101楼2017-04-30 23:48
                      回复
                        18、添加/删除地面叠加层
                        <!DOCTYPE html>
                        <html>
                        <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                        <style type="text/css">
                        body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
                        #allmap{height:500px;width:100%;}
                        #r-result{width:100%;}
                        </style>
                        <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                        <title>添加/删除地面叠加层</title>
                        </head>
                        <body>
                        <div id="allmap"></div>
                        <div id="r-result">
                        <input type="button" onclick="addGroundOverlay();" value="添加" />
                        <input type="button" onclick="removeGroundOverlay();" value="删除" />
                        </div>
                        </body>
                        </html>
                        <script type="text/javascript">
                        var TianAnMen = new BMap.Point(116.404449, 39.914889);
                        // 创建Map实例
                        var map = new BMap.Map("allmap");
                        // 居中放大
                        map.centerAndZoom(TianAnMen, 12);
                        // 启用滚轮放大缩小
                        map.enableScrollWheelZoom();
                        // 西南角和东北角
                        var SW = new BMap.Point(116.29579,39.837146);
                        var NE = new BMap.Point(116.475451,39.9764);
                        groundOverlayOptions = {
                        opacity: 1,
                        displayOnMinLevel: 10,
                        displayOnMaxLevel: 14
                        }
                        // 初始化GroundOverlay
                        var groundOverlay = new BMap.GroundOverlay(new BMap.Bounds(SW, NE), groundOverlayOptions);
                        // 设置GroundOverlay的图片地址
                        groundOverlay.setImageURL('http://developer.baidu.com/map/jsdemo/img/si-huan.bmp');
                        // 单击事件
                        groundOverlay.addEventListener('click', function (clickEvent) {
                        // console.log('clickEvent', clickEvent);
                        });
                        // 双击事件
                        groundOverlay.addEventListener('dblclick', function (dblclickEvent) {
                        // console.log('dblclickEvent', dblclickEvent);
                        });
                        function addGroundOverlay() {
                        // 添加GroundOverlay
                        map.addOverlay(groundOverlay);
                        }
                        function removeGroundOverlay() {
                        // 移除GroundOverlay
                        map.removeOverlay(groundOverlay);
                        }
                        </script>


                        IP属地:浙江102楼2017-04-30 23:52
                        回复
                          19、点聚合
                          <!DOCTYPE html>
                          <html>
                          <head>
                          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                          <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                          <style type="text/css">
                          body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
                          #allmap{width:100%;height:500px;}
                          p{margin-left:5px; font-size:14px;}
                          </style>
                          <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                          <script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
                          <script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
                          <title>点聚合</title>
                          </head>
                          <body>
                          <div id="allmap"></div>
                          <p>缩放地图,查看点聚合效果</p>
                          </body>
                          </html>
                          <script type="text/javascript">
                          // 百度地图API功能
                          var map = new BMap.Map("allmap");
                          map.centerAndZoom(new BMap.Point(116.404, 39.915), 4);
                          map.enableScrollWheelZoom();
                          var MAX = 10;
                          var markers = [];
                          var pt = null;
                          var i = 0;
                          for (; i < MAX; i++) {
                          pt = new BMap.Point(Math.random() * 40 + 85, Math.random() * 30 + 21);
                          markers.push(new BMap.Marker(pt));
                          }
                          //最简单的用法,生成一个marker数组,然后调用markerClusterer类即可。
                          var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
                          </script>


                          IP属地:浙江103楼2017-05-01 00:39
                          回复
                            20、添加热力图
                            <!DOCTYPE html>
                            <html>
                            <head>
                            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                            <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                            <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
                            <script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"></script>
                            <title>热力图功能示例</title>
                            <style type="text/css">
                            ul,li{list-style: none;margin:0;padding:0;float:left;}
                            html{height:100%}
                            body{height:100%;margin:0px;padding:0px;font-family:"微软雅黑";}
                            #container{height:500px;width:100%;}
                            #r-result{width:100%;}
                            </style>
                            </head>
                            <body>
                            <div id="container"></div>
                            <div id="r-result">
                            <input type="button" onclick="openHeatmap();" value="显示热力图"/><input type="button" onclick="closeHeatmap();" value="关闭热力图"/>
                            </div>
                            </body>
                            </html>
                            <script type="text/javascript">
                            var map = new BMap.Map("container"); // 创建地图实例
                            var point = new BMap.Point(116.418261, 39.921984);
                            map.centerAndZoom(point, 15); // 初始化地图,设置中心点坐标和地图级别
                            map.enableScrollWheelZoom(); // 允许滚轮缩放
                            var points =[
                            {"lng":116.418261,"lat":39.921984,"count":50},
                            {"lng":116.423332,"lat":39.916532,"count":51},
                            {"lng":116.419787,"lat":39.930658,"count":15},
                            {"lng":116.418455,"lat":39.920921,"count":40},
                            {"lng":116.418843,"lat":39.915516,"count":100},
                            {"lng":116.42546,"lat":39.918503,"count":6},
                            {"lng":116.423289,"lat":39.919989,"count":18},
                            {"lng":116.418162,"lat":39.915051,"count":80},
                            {"lng":116.422039,"lat":39.91782,"count":11},
                            {"lng":116.41387,"lat":39.917253,"count":7},
                            {"lng":116.41773,"lat":39.919426,"count":42},
                            {"lng":116.421107,"lat":39.916445,"count":4},
                            {"lng":116.417521,"lat":39.917943,"count":27},
                            {"lng":116.419812,"lat":39.920836,"count":23},
                            {"lng":116.420682,"lat":39.91463,"count":60},
                            {"lng":116.415424,"lat":39.924675,"count":8},
                            {"lng":116.419242,"lat":39.914509,"count":15},
                            {"lng":116.422766,"lat":39.921408,"count":25},
                            {"lng":116.421674,"lat":39.924396,"count":21},
                            {"lng":116.427268,"lat":39.92267,"count":1},
                            {"lng":116.417721,"lat":39.920034,"count":51},
                            {"lng":116.412456,"lat":39.92667,"count":7},
                            {"lng":116.420432,"lat":39.919114,"count":11},
                            {"lng":116.425013,"lat":39.921611,"count":35},
                            {"lng":116.418733,"lat":39.931037,"count":22},
                            {"lng":116.419336,"lat":39.931134,"count":4},
                            {"lng":116.413557,"lat":39.923254,"count":5},
                            {"lng":116.418367,"lat":39.92943,"count":3},
                            {"lng":116.424312,"lat":39.919621,"count":100},
                            {"lng":116.423874,"lat":39.919447,"count":87},
                            {"lng":116.424225,"lat":39.923091,"count":32},
                            {"lng":116.417801,"lat":39.921854,"count":44},
                            {"lng":116.417129,"lat":39.928227,"count":21},
                            {"lng":116.426426,"lat":39.922286,"count":80},
                            {"lng":116.421597,"lat":39.91948,"count":32},
                            {"lng":116.423895,"lat":39.920787,"count":26},
                            {"lng":116.423563,"lat":39.921197,"count":17},
                            {"lng":116.417982,"lat":39.922547,"count":17},
                            {"lng":116.426126,"lat":39.921938,"count":25},
                            {"lng":116.42326,"lat":39.915782,"count":100},
                            {"lng":116.419239,"lat":39.916759,"count":39},
                            {"lng":116.417185,"lat":39.929123,"count":11},
                            {"lng":116.417237,"lat":39.927518,"count":9},
                            {"lng":116.417784,"lat":39.915754,"count":47},
                            {"lng":116.420193,"lat":39.917061,"count":52},
                            {"lng":116.422735,"lat":39.915619,"count":100},
                            {"lng":116.418495,"lat":39.915958,"count":46},
                            {"lng":116.416292,"lat":39.931166,"count":9},
                            {"lng":116.419916,"lat":39.924055,"count":8},
                            {"lng":116.42189,"lat":39.921308,"count":11},
                            {"lng":116.413765,"lat":39.929376,"count":3},
                            {"lng":116.418232,"lat":39.920348,"count":50},
                            {"lng":116.417554,"lat":39.930511,"count":15},
                            {"lng":116.418568,"lat":39.918161,"count":23},
                            {"lng":116.413461,"lat":39.926306,"count":3},
                            {"lng":116.42232,"lat":39.92161,"count":13},
                            {"lng":116.4174,"lat":39.928616,"count":6},
                            {"lng":116.424679,"lat":39.915499,"count":21},
                            {"lng":116.42171,"lat":39.915738,"count":29},
                            {"lng":116.417836,"lat":39.916998,"count":99},
                            {"lng":116.420755,"lat":39.928001,"count":10},
                            {"lng":116.414077,"lat":39.930655,"count":14},
                            {"lng":116.426092,"lat":39.922995,"count":16},
                            {"lng":116.41535,"lat":39.931054,"count":15},
                            {"lng":116.413022,"lat":39.921895,"count":13},
                            {"lng":116.415551,"lat":39.913373,"count":17},
                            {"lng":116.421191,"lat":39.926572,"count":1},
                            {"lng":116.419612,"lat":39.917119,"count":9},
                            {"lng":116.418237,"lat":39.921337,"count":54},
                            {"lng":116.423776,"lat":39.921919,"count":26},
                            {"lng":116.417694,"lat":39.92536,"count":17},
                            {"lng":116.415377,"lat":39.914137,"count":19},
                            {"lng":116.417434,"lat":39.914394,"count":43},
                            {"lng":116.42588,"lat":39.922622,"count":27},
                            {"lng":116.418345,"lat":39.919467,"count":8},
                            {"lng":116.426883,"lat":39.917171,"count":3},
                            {"lng":116.423877,"lat":39.916659,"count":34},
                            {"lng":116.415712,"lat":39.915613,"count":14},
                            {"lng":116.419869,"lat":39.931416,"count":12},
                            {"lng":116.416956,"lat":39.925377,"count":11},
                            {"lng":116.42066,"lat":39.925017,"count":38},
                            {"lng":116.416244,"lat":39.920215,"count":91},
                            {"lng":116.41929,"lat":39.915908,"count":54},
                            {"lng":116.422116,"lat":39.919658,"count":21},
                            {"lng":116.4183,"lat":39.925015,"count":15},
                            {"lng":116.421969,"lat":39.913527,"count":3},
                            {"lng":116.422936,"lat":39.921854,"count":24},
                            {"lng":116.41905,"lat":39.929217,"count":12},
                            {"lng":116.424579,"lat":39.914987,"count":57},
                            {"lng":116.42076,"lat":39.915251,"count":70},
                            {"lng":116.425867,"lat":39.918989,"count":8}];
                            if(!isSupportCanvas()){
                            alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
                            }


                            IP属地:浙江104楼2017-05-01 00:39
                            回复
                              //详细的参数,可以查看heatmap.js的文档 https://github.com/pa7/heatmap.js/blob/master/README.md
                              //参数说明如下:
                              /* visible 热力图是否显示,默认为true
                              * opacity 热力的透明度,1-100
                              * radius 势力图的每个点的半径大小
                              * gradient {JSON} 热力图的渐变区间 . gradient如下所示
                              *{
                              .2:'rgb(0, 255, 255)',
                              .5:'rgb(0, 110, 255)',
                              .8:'rgb(100, 0, 255)'
                              }
                              其中 key 表示插值的位置, 0~1.
                              value 为颜色值.
                              */
                              heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20});
                              map.addOverlay(heatmapOverlay);
                              heatmapOverlay.setDataSet({data:points,max:100});
                              //是否显示热力图
                              function openHeatmap(){
                              heatmapOverlay.show();
                              }
                              function closeHeatmap(){
                              heatmapOverlay.hide();
                              }
                              closeHeatmap();
                              function setGradient(){
                              /*格式如下所示:
                              {
                              0:'rgb(102, 255, 0)',
                              .5:'rgb(255, 170, 0)',
                              1:'rgb(255, 0, 0)'
                              }*/
                              var gradient = {};
                              var colors = document.querySelectorAll("input[type='color']");
                              colors = [].slice.call(colors,0);
                              colors.forEach(function(ele){
                              gradient[ele.getAttribute("data-key")] = ele.value;
                              });
                              heatmapOverlay.setOptions({"gradient":gradient});
                              }
                              //判断浏览区是否支持canvas
                              function isSupportCanvas(){
                              var elem = document.createElement('canvas');
                              return !!(elem.getContext && elem.getContext('2d'));
                              }
                              </script>


                              IP属地:浙江105楼2017-05-01 00:41
                              回复