百度地图画多边形代码

百度地图画多边形代码

<!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; overflow: hidden;}  
    #result {width:100%;font-size:12px;}  
    dl,dt,dd,ul,li{  
        margin:0;  
        padding:0;  
        list-style:none;  
    }  
    p{font-size:12px;}  
    dt{  
        font-size:14px;  
        font-family:"微软雅黑";  
        font-weight:bold;  
        border-bottom:1px dotted #000;  
        padding:5px 0 5px 5px;  
        margin:5px 0;  
    }  
    dd{  
        padding:5px 0 0 5px;  
    }  
    li{  
        line-height:28px;  
    }  
    </style>  
     <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=F0i6SrLmHquLVNLCqpExxPrj8mWVdFwx"></script>  
    <!--加载鼠标绘制工具-->  
    <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>  
    <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />  
    <!--加载检索信息窗口-->  
    <script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>  
    <link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />  
    <title>百度地图API功能演示</title>  
</head>  
<body>  
    <div id="allmap" style="overflow:hidden;zoom:1;position:relative;">     
        <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;">  
       </div>  
    </div>  
    <div id="result">  
        <input type="button" value="获取绘制的覆盖物个数" onclick="alert(overlays.length)"/>  
        <input type="button" value="清除所有覆盖物" onclick="clearAll()"/>  
        <input type="button"  value="获取多边形的顶点" onclick="getPoint()"/><br/>  
        <input type="button"  value="开启线、面编辑功能" onclick="Editing('enable')"/>  
        <input type="button"  value="关闭线、面编辑功能" onclick="Editing('disable')"/>  
        <input type="button"  value="显示原有多边形" onclick="showPolygon(this)" /><br/>  

        <input type="button"  value="画多边形" onclick="draw(BMAP_DRAWING_POLYGON)" />  
         <input type="button"  value="画矩形" onclick="draw(BMAP_DRAWING_RECTANGLE)" />  
          <input type="button"  value="画线" onclick="draw(BMAP_DRAWING_POLYLINE)" />   
          <!-- <input type="button"  value="画点" onclick="draw(BMAP_DRAWING_MARKER)" />-->  
          <span>右键获取任意点的经纬度</span>  
    </div>  
    <div id="resultShape"></div>  
     <div id="shape">坐标为</div>  

<script type="text/javascript">  
    function $(id){  
    return document.getElementById(id);  
}  

    // 百度地图API功能  
    var map = new BMap.Map('map');  
    var poi = new BMap.Point(113.948913,22.530844);  
    map.centerAndZoom(poi, 16);  
    map.enableScrollWheelZoom();    
    var overlays = [];  
    var overlaycomplete = function(e){  
        overlays.push(e.overlay);   
    };  
    var styleOptions = {  
        strokeColor:"red",    //边线颜色。  
        fillColor:"red",      //填充颜色。当参数为空时,圆形将没有填充效果。  
        strokeWeight: 3,       //边线的宽度,以像素为单位。  
        strokeOpacity: 0.8,    //边线透明度,取值范围0 - 1。  
        fillOpacity: 0.6,      //填充的透明度,取值范围0 - 1。  
        strokeStyle: 'solid' //边线的样式,solid或dashed。  
    }  

    //实例化鼠标绘制工具  
    var drawingManager = new BMapLib.DrawingManager(map, {  
        isOpen: false, //是否开启绘制模式  
        //enableDrawingTool: true, //是否显示工具栏  
        drawingToolOptions: {  
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置  
            offset: new BMap.Size(5, 5), //偏离值  
        },  
        circleOptions: styleOptions, //圆的样式  
        polylineOptions: styleOptions, //线的样式  
        polygonOptions: styleOptions, //多边形的样式  
        rectangleOptions: styleOptions //矩形的样式  
    });    

     //添加鼠标绘制工具监听事件,用于获取绘制结果  
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);  

    map.addEventListener("rightclick",function(e){  
        if(confirm(e.point.lng + "," + e.point.lat)){  
            $("shape").innerHTML=$("shape").innerHTML+" <br/>("+e.point.lng+","+e.point.lat+")";  
            }  
        });  
    function draw(type){  
        drawingManager.open();   
        drawingManager.setDrawingMode(type);  
    }     

    function clearAll() {  
        for(var i = 0; i < overlays.length; i++){  
            map.removeOverlay(overlays[i]);  
        }  
        overlays.length = 0     
    }  
    function getPoint(){  
        $("resultShape").innerHTML='';  
        for(var i = 0; i < overlays.length; i++){  
            var overlay=overlays[i].getPath();  
            $("resultShape").innerHTML=$("resultShape").innerHTML+overlay.length+'边形:<br/>';  
            for(var j = 0; j < overlay.length; j++){  
                var grid =overlay[j];  
                $("resultShape").innerHTML=$("resultShape").innerHTML+(j+1)+"个点:("+grid.lng+","+grid.lat+");<br/>";  
            }  
        }  
    }  
    function Editing(state){  
        for(var i = 0; i < overlays.length; i++){  
            state=='enable'?overlays[i].enableEditing():overlays[i].disableEditing();  
        }  
    }  

    function  showPolygon(btn){  
      var polygon = new BMap.Polygon([  
          new BMap.Point(113.941853,22.530777),  
          new BMap.Point(113.940487,22.527789),  
          new BMap.Point(113.94788,22.527597),  
          new BMap.Point(113.947925,22.530618)  
      ], styleOptions);  //创建多边形  
      map.addOverlay(polygon);   //增加多边形  
      // overlays.push(polygon); //是否把该图像加入到编辑和删除行列  
       btn.setAttribute('disabled','false');  
       showText();  
     }  

     function showText(){  
        var opts = {  
        position : new BMap.Point(113.941853,22.530777),    // 指定文本标注所在的地理位置  
        offset   : new BMap.Size(30, 30)    //设置文本偏移量  
       }  
     var label = new BMap.Label("不可编辑", opts);  // 创建文本标注对象  
        label.setStyle({  
        color : "black",  
         fontSize : "16px",  
        fillColor:"red",      //填充颜色。当参数为空时,圆形将没有填充效果。  
        });  
      map.addOverlay(label);    
     }  
</script>  
</body>  
</html>  

java判断点是否在多边形中

/**
 * 判断点是否在多边形内
 * @param point 检测点
 * @param pts   多边形的顶点
 * @return      点在多边形内返回true,否则返回false
 */
public static boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts){

    int N = pts.size();
    boolean boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
    int intersectCount = 0;//cross points count of x 
    double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
    Point2D.Double p1, p2;//neighbour bound vertices
    Point2D.Double p = point; //当前点

    p1 = pts.get(0);//left vertex        
    for(int i = 1; i <= N; ++i){//check all rays            
        if(p.equals(p1)){
            return boundOrVertex;//p is an vertex
        }

        p2 = pts.get(i % N);//right vertex            
        if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){//ray is outside of our interests                
            p1 = p2; 
            continue;//next ray left point
        }

        if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
            if(p.y <= Math.max(p1.y, p2.y)){//x is before of ray                    
                if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
                    return boundOrVertex;
                }

                if(p1.y == p2.y){//ray is vertical                        
                    if(p1.y == p.y){//overlies on a vertical ray
                        return boundOrVertex;
                    }else{//before ray
                        ++intersectCount;
                    } 
                }else{//cross point on the left side                        
                    double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y                        
                    if(Math.abs(p.y - xinters) < precision){//overlies on a ray
                        return boundOrVertex;
                    }

                    if(p.y < xinters){//before ray
                        ++intersectCount;
                    } 
                }
            }
        }else{//special case when ray is crossing through the vertex                
            if(p.x == p2.x && p.y <= p2.y){//p crossing over p2                    
                Point2D.Double p3 = pts.get((i+1) % N); //next vertex                    
                if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
                    ++intersectCount;
                }else{
                    intersectCount += 2;
                }
            }
        }            
        p1 = p2;//next ray left point
    }

    if(intersectCount % 2 == 0){//偶数在多边形外
        return false;
    } else { //奇数在多边形内
        return true;
    }

}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值