Javascript 传单::如何检查点位于多边形或矩形的内部/外部
是否有任何算法来检查标记位于多边形、矩形和圆的内部或外部。我试着用这个写一个函数。但是没有成功。如果您使用的是PHP,那么这个函数可以工作Javascript 传单::如何检查点位于多边形或矩形的内部/外部,javascript,maps,leaflet,Javascript,Maps,Leaflet,是否有任何算法来检查标记位于多边形、矩形和圆的内部或外部。我试着用这个写一个函数。但是没有成功。如果您使用的是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 =
$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;
}
$c=false;
$vertices_x=数组(22.333,22.222,22444)//多边形的纬度点
$vertices_y=数组(75.111,75.2222,76.233)//多边形经度点
$points\u polygon=计数($textexts\u x);
$longitude=23.345//待检查点的纬度
$LATIONE=75.123//待检查点的经度
if(是多边形中的($points\u polygon,$vertices\u x,$vertices\u y,$longitude,$latitude)){
回声“在多边形中!”“
”;
}
否则{
回声“不在多边形中”;
}
函数是多边形中的($points\u polygon,$vertices\u x,$vertices\u y,$longitude\u x,$latitude\u y){
$i=$j=$c=0;
对于($i=0,$j=$points\u polygon-1;$i<$points\u polygon;$j=$i++){
如果($vertices_y[$i]>$latitude_y!=($vertices_y[$j]>$latitude_y))&($latitude_x<($vertices_x[$j]-$vertices_x[$i])*($latitude_y-$vertices_y[$i]-$vertices_y[$i])+$vertices_x[$i])){
$c=!$c;
}
}
返回$c;
}
传单中有一个功能可以检查这一点
Polygon.getBounds().contains(MarketLatLng);
为了体现其价值,传单API使用contains
函数为矩形提供边界检查。不过,多边形和圆没有用。是一个传单附加组件,提供了一个实现光线跟踪技术的函数,用于确定点是否位于多边形内。可接受的答案应为传单-pip。工作正常(修补源文件后:删除“l.MultiPolygon的l instanceof l.MultiPolygon”),谢谢。。但是你的代码可以和谷歌API一起工作。我想用单张APIs检查点。这只检查点是否在多边形的边界矩形内,而不是多边形本身是否包含该点。为了使其更智能,请使用光线投射算法()。可用的javascript代码。此函数是否也适用于矩形?
<!-- <!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> -->