简体   繁体   English

openlayers如何检测标记是否在多边形内

[英]openlayers How to detect if a marker is within a polygon

Does anyone know if there is an easy way to detect if a marker is within a defined boundary? 有没有人知道是否有一种简单的方法来检测标记是否在定义的边界内?

thanks 谢谢

I'm assuming you are using OL v2. 我假设你正在使用OL v2。

On one side, the marker requires you to specify a OpenLayers.LonLat object. 一方面,标记要求您指定OpenLayers.LonLat对象。 On the other hand, you can define any boundary with the OpenLayers.Bounds class and then check if it contains the marker LonLat with containsLonLat method. 另一方面,您可以使用OpenLayers.Bounds类定义任何边界,然后检查它是否包含带有containsLonLat方法的标记LonLat。 (check http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/BaseTypes/Bounds-js.html ). (查看http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/BaseTypes/Bounds-js.html )。

In the same way if you work with geometries like points, linestring, etc you have methods like intersect to check this. 以同样的方式,如果你使用像点,线串等几何,你有像十字架这样的方法来检查这个。

i have sample code done by me which check marker point is in within polygon or not. 我有我做的示例代码,检查标记点是否在多边形内。

function init() {

        map = new OpenLayers.Map("mapdiv");
        map.addLayer(new OpenLayers.Layer.OSM());

        epsg4326 = new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
        projectTo = map.getProjectionObject(); //The map projection (Spherical Mercator)
        epsg900913 = new OpenLayers.Projection("EPSG:900913");

        var lonLat = new OpenLayers.LonLat(lon, lat).transform(epsg4326, projectTo);

        map.setCenter(lonLat, zoom);
        map.addControl(new OpenLayers.Control.MousePosition());
        var vectorLayer = new OpenLayers.Layer.Vector("Overlay");

        var point = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);

        var myPolygon = OpenLayers.Geometry.Polygon.createRegularPolygon
        (
            point,
            5000,
            4,
            0
        );

        var featurecircle = new OpenLayers.Feature.Vector(myPolygon);
        var markers = new OpenLayers.Layer.Markers( ".\marker.png" );
            map.addLayer(markers);
            markers.addMarker(new OpenLayers.Marker(lonLat));

        controls = {

                drag: new OpenLayers.Control.DragFeature(vectorLayer, {
                     autoActivate: true,
                     onComplete: displayBounds
                  })

                  //new OpenLayers.Control.DragFeature(vectorLayer)
            };

            for(var key in controls) {
                map.addControl(controls[key]);
            }

        var featurePoint = new OpenLayers.Feature.Vector(
            point,
            { description: 'info' },
            { externalGraphic: 'img/marker.png', graphicHeight: 25, graphicWidth: 21, graphicXOffset: -12, graphicYOffset: -25 }
        );

        vectorLayer.addFeatures([featurecircle]);
        for(key in controls) {
                var control = controls[key];
                control.activate();
                }
                var bounds = vectorLayer.features[0].geometry.getBounds();

    function displayBounds(feature){
            var bounds = feature.geometry.getBounds();
            var lonlat = new OpenLayers.LonLat(lon, lat).transform(epsg4326,epsg900913);;


            if ((lonlat.lon < bounds.left) || (lonlat.lat > bounds.top) || (lonlat.lat < bounds.bottom) ||(lonlat.lon > bounds.right) )
                {
                alert('out....');

                }

        }




        map.addLayer(vectorLayer);
  }

i solve this with 我解决这个问题

var poly = new OpenLayers.Geometry.Polygon([linearRing]);
                var polygonFeature = new OpenLayers.Feature.Vector(poly, null, siteStyle);
                map.addLayer(vectors);
                vectors.addFeatures([polygonFeature]);
var marker_point=new OpenLayers.Geometry.Point(lng,lat);
            var inside=poly.containsPoint(marker_point);
            if(inside)alert("Inside the polygon");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM