百度地图API-调用后台接口画出点位
更新:HHH   时间:2023-1-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 src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
    <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实例
    map.centerAndZoom(new BMap.Point(110.28670182010947, 20.004970779693032), 10);  // 初始化地图,设置中心点坐标和地图级别
    //添加地图类型控件
    map.addControl(new BMap.MapTypeControl({
        mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));     
    map.setCurrentCity("海口");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

    $.ajax({url:"http://114.55.xx.xx:xxxx/cloud/rygl/api/np/v2/cloud/getStaffArea.read?tenantId=5bb0e1c08f254e499e1e5973eb82f294&staffId=f1d5970e0abd4f5db0b9a0e03915cff6&dateStr=2018-06-21",success:function(result){
          var str="";
          var lists=result.data;
           $.each(lists,function(index,dataItem){
            $.each(dataItem,function(index,item){
               var points;
               points=new Array();
               var color=randomHexColor();
               $.each(item,function(index,iItem){
                  var tempPoint=new BMap.Point(iItem.lngDone,iItem.latDone);
                  points.push(tempPoint); 
                 });
               var polyline = new BMap.Polyline(points, {strokeColor:color, strokeWeight:5, strokeOpacity:1});
                  map.addOverlay(polyline);
            });
          });
          alert(result.msg);
        }});

    function randomHexColor() { //随机生成十六进制颜色
     var hex = Math.floor(Math.random() * 16777216).toString(16); //生成ffffff以内16进制数
     while (hex.length < 6) { //while循环判断hex位数,少于6位前面加0凑够6位
      hex = '0' + hex;
     }
     return '#' + hex; //返回‘#'开头16进制颜色
    }

</script>
返回web开发教程...