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

时间:2013-06-03 05:04:56

标签: javascript maps leaflet

是否有任何算法可以检查多边形,矩形和圆形内部或外部的标记。我尝试使用此link编写函数。但没有成功。

3 个答案:

答案 0 :(得分:19)

传单中有一个功能可以检查这个。

Polygon.getBounds().contains(MarketLatLng);

答案 1 :(得分:5)

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

答案 2 :(得分: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> -->