Leaflet:如何检查点是否在多边形或矩形内部/外部

26

是否有一种算法可以检查标记(marker)是否位于多边形、矩形或圆形内部或外部。我尝试使用这个 链接编写了一个函数,但是没有成功。


就算不值得一提,Leaflet API 提供了使用 contains 函数进行矩形边界检查的功能。http://leafletjs.com/reference.html#bounds 不过对于多边形和圆形没有用处。 - Patrick D
4
Leaflet-pip 是 Leaflet 的一个插件,提供了一种使用射线追踪技术来判断点是否在多边形内的函数。 - David K. Hess
1
leaflet-pip 应该是被接受的答案。修补源文件后可以正常工作(删除“l instanceof L.MultiPolygon”,https://github.com/mapbox/leaflet-pip/issues/8)。 - AlainIb
3个回答

25

Leaflet中有一个函数可以检查这个。

Polygon.getBounds().contains(MarketLatLng);

32
这只会检查该点是否在多边形的外接矩形内,而不是检查它是否被多边形本身包含。 - David K. Hess
4
为了使其更智能,使用射线投射算法(可在此处解释:http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html)。 实现示例 - szymonm
可用的工作JavaScript代码在此处 - Aneesh
它的工作不正确,导致错误的结果。例如:应该返回false,但却返回true。L.polygon(arr).getBounds().contains([35.777846741014, 51.371061187093]),其中arr=[{lat:36.131196609314266, lng:51.34257481466077},{lat:36.0685037997188, lng:51.33158848653577},{lat:35.95241809543124, lng:51.31167576680921},{lat:35.80498248907691, lng:51.55337498555921},{lat:35.768777146507226, lng:51.65019200216077},{lat:35.767105732568375, lng:51.79026768575452},{lat:35.848407125075894, lng:51.884338120324834},{lat:36.0585127164683, lng:51.620666245324834},]; - mehrdad seyrafi

6
如果您在使用PHP,那么这个函数就可以运行。
$c = false; 

$vertices_x = array(22.333,22.222,22,444);  //latitude points of polygon
$vertices_y = array(75.111,75.2222,76.233);   //longitude points of polygon
$points_polygon = count($vertices_x); 
$longitude =  23.345; //latitude of point to be checked
$latitude =  75.123; //longitude of point to be checked

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude, $latitude)){
    echo "Is in polygon!"."<br>";
}
else { 
    echo "Is not in polygon"; 
}

function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y) {
    $i = $j = $c = 0;

    for ($i = 0, $j = $points_polygon-1; $i < $points_polygon; $j = $i++) {
        if (($vertices_y[$i] >  $latitude_y != ($vertices_y[$j] > $latitude_y)) && ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i])) {
            $c = !$c;
        }
    }

    return $c;
}

这个函数也适用于矩形吗? - user2485649

0
<!-- <!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
       #map-canvas {
        margin: 0;
        padding: 0;
        width: 700px;
        height: 500px;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var map;
var marker;
var latlong = [["21.001663","75.486069"],
["20.108977","73.914672"],
["21.1458","79.088155"],
["19.153061","77.305847"],
["20.0831","73.79095"],
["18.52043","73.856744"],
["16.774832","74.486265"],
["16.691308","74.244866"],
["19.876165","75.343314"],
["19.997453","73.789802"],
["20.532949","76.184303"],
["21.013321","75.563972"],
["18.9513","72.82831"],
["18.515752","73.182162"],
["19.075984","72.877656"],
["19.218331","72.97809"],
["19.844006","79.36266"],
["20.745319","78.602195"],
["21.267052","78.577973"],
["18.52043","73.856744"],
["19.96955","79.304654"],
["19.450585","72.799155"],
["18.52043","73.856744"],
["20.745319","78.602195"],
["18.9833","75.7667"],
["21.013321","75.563972"],
["21.1458","79.088155"],
["19.153061","77.305847"]];

function initialize() {
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(21.7679, 78.8718),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
      drawCircle();
}
function drawCircle() 
{
var options = {
        strokeColor: '#800000',
        strokeOpacity: 1.0,
        strokeWeight: 1,
        fillColor: '#C64D45',
        fillOpacity: 0.5,
        map: map,
        center: new google.maps.LatLng(21.7679, 78.8718),
        radius: 100000
    };

    circle = new google.maps.Circle(options);
    var bounds= circle.getBounds(); 
    for (var i = 0; i < latlong.length; i++){
    var hello =new google.maps.LatLng(latlong[i][0], latlong[i][1]);
    if(bounds.contains(hello)) 
    {
       marker= new google.maps.Marker({
      position:hello,
       });
       marker.setMap(map);
          console.log("Hi iam in bound");

          }
          else
          {
             console.log("Iam not in bound");
}
}

}
google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html> -->

2
谢谢,但是你的代码使用的是Google APIs。我想要使用Leaflet APIs来进行检查点。 - user2446592

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接