首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Leaflet::如何检查点位于多边形或矩形的内部/外部

Leaflet::如何检查点位于多边形或矩形的内部/外部
EN

Stack Overflow用户
提问于 2013-06-03 13:04:56
回答 3查看 26.1K关注 0票数 23

是否有任何算法来检查标记位于多边形、矩形和圆的内部或外部。我试着用这个link写一个函数。但没有成功。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-04 14:48:42

如果您使用的是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;
}
票数 6
EN

Stack Overflow用户

发布于 2013-08-16 02:17:57

在leaflet中有一个函数可以检查这一点。

Polygon.getBounds().contains(MarketLatLng);
票数 22
EN

Stack Overflow用户

发布于 2013-08-19 19:52:36

<!-- <!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> -->
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16890294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档