给定表示目标的长/纬度坐标的数据库和代表表示“区域”的多边形的边界坐标的一系列经纬度。使用简单的光线投射算法将经纬度视为笛卡尔坐标以确定目标是否在区域内是安全的?纬度和经度作为坐标适合于多边形算法中的点
0
A
回答
2
如果边缘很短,它应该工作得很好。但它不会是“正确的”。
想象一个三角形,赤道上有两个点a和b,一个与北极短。显然,赤道上方a和b之间的经度都在三角形中(几乎)。但是,如果我们尝试将lats/longs视为笛卡尔坐标,我们会得到一些完全不同的东西...
编辑:您需要注意跨越经度= max/min的区域,并且更加小心那些还包含杆的球杆
0
不,这样做不安全。你会得到极点周围以及0经度地区的异常情况。
看看美国宇航局的世界风代码。他们对地球表面上的多边形有表示,尽管你必须小心 - 一些表示会代表这些异常,而另一些则不会。
1
你可以试试这个
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBfaExFhABnh7I3-vTHJjA4TWqtYnMccKE&sensor=false"></script>
<script type="text/javascript" src="http://www.the-di-lab.com/polygon/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
var map;
var boundaryPolygon;
function initialize() {
google.maps.Polygon.prototype.Contains = function (point) {
// ray casting alogrithm http://rosettacode.org/wiki/Ray-casting_algorithm
var crossings = 0,
path = this.getPath();
// for each edge
for (var i = 0; i < path.getLength(); i++) {
var a = path.getAt(i),
j = i + 1;
if (j >= path.getLength()) {
j = 0;
}
var b = path.getAt(j);
if (rayCrossesSegment(point, a, b)) {
crossings++;
}
}
// odd number of crossings?
return (crossings % 2 == 1);
function rayCrossesSegment(point, a, b) {
var px = point.lng(),
py = point.lat(),
ax = a.lng(),
ay = a.lat(),
bx = b.lng(),
by = b.lat();
if (ay > by) {
ax = b.lng();
ay = b.lat();
bx = a.lng();
by = a.lat();
}
if (py == ay || py == by) py += 0.00000001;
if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
if (px < Math.min(ax, bx)) return true;
var red = (ax != bx) ? ((by - ay)/(bx - ax)) : Infinity;
var blue = (ax != px) ? ((py - ay)/(px - ax)) : Infinity;
return (blue >= red);
}
};
var mapProp = {
center: new google.maps.LatLng(37.684, -122.591),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
google.maps.event.addListener(map, 'click', function (event) {
if (boundaryPolygon!=null && boundaryPolygon.Contains(event.latLng)) {
alert(event.latLng + " is inside the polygon");
} else {
alert(event.latLng + " is outside the polygon");
}
});
}
function btnCheckPointExistence_onclick() {
var latitude = document.getElementById('txtLattitude').value;
var longitude = document.getElementById('txtLongitude').value;
//alert(latitude); alert(longitude)
var myPoint = new google.maps.LatLng(latitude, longitude);
if (boundaryPolygon == null) {
alert("No Polygon");
}
else {
if (boundaryPolygon.Contains(myPoint)) {
alert(myPoint + "is inside the polygon.");
} else {
alert(myPoint + "is outside the polygon.");
}
}
}
function drawPolygon() {
initialize();
var boundary = '77.702866 28.987153, 77.699776 28.978594 ,77.735996 28.974164 ,77.719946 28.99346 ,77.713423 28.994361 ,77.711706 28.990382 ';
var boundarydata = new Array();
var latlongs = boundary.split(",");
for (var i = 0; i < latlongs.length; i++) {
latlong = latlongs[i].trim().split(" ");
boundarydata[i] = new google.maps.LatLng(latlong[1], latlong[0]);
}
boundaryPolygon = new google.maps.Polygon({
path: boundarydata,
strokeColor: "#0000FF",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: 'Red',
fillOpacity: 0.4
});
google.maps.event.addListener(boundaryPolygon, 'click', function (event) {
if (boundaryPolygon.Contains(event.latLng)) {
alert(event.latLng + "is inside the polygon.");
} else {
alert(event.latLng + "is outside the polygon.");
}
});
map.setZoom(14);
map.setCenter(boundarydata[0]);
boundaryPolygon.setMap(map);
}
</script>
</head>
<body onload="initialize();drawPolygon();">
<form id="form1" runat="server">
<input type="text" value="28.982005022927137" name="txtLattitude" id="txtLattitude" placeholder="Enter Latitude">
<input type="text" value="77.72157669067383" name="txtLongitude" id="txtLongitude" placeholder="Enter Langitude">
<input type="button" value="Submit" name="submit" id="submit-text" value="submit" onclick="btnCheckPointExistence_onclick();">
<h3>Check If Point Exist in a Polygon</h3>
<span id="spnMsg" style="font-family: Arial; text-align: center; font-size: 14px; color: red;">this is message</span>
<br />
<br />
<div id="googleMap" style="width: auto; height: 500px;">
</div>
</form>
</body>
</html>
您可以查看Demo here
相关问题
- 1. 点上多边形(经度/纬度)
- 2. 以PHP计算多边形的纬度经度中心点
- 3. 经纬度坐标
- 4. 添加和翻译纬度/经度坐标的算法
- 5. PHP比较经度/纬度边界坐标点
- 6. 纬度和经度UTM坐标
- 7. 3D坐标经度和纬度
- 8. 在纬度/经度点之间更多地计算经度和纬度?
- 9. 用于确定收集经度/纬度坐标的最小边界矩形的算法
- 10. 如何获取方法viewDidLoad中的纬度和经度坐标?
- 11. 从坐标数据转换为R中的经度和纬度?
- 12. 排序纬度和经度坐标转换成顺时针排列四边形
- 13. 争夺纬度经度坐标的JavaScript
- 14. 计算边界框距离Java中的纬度/经度坐标一定距离
- 15. 将纬度经度坐标转换为图像地图坐标
- 16. Shape文件:XY坐标和经度/纬度坐标
- 17. 经度,纬度到XY坐标转换
- 18. 随机纬度经度坐标
- 19. GoogleMaps API - 坐标(经度,纬度)地址
- 20. 计算距离另一纬度/经度点米的纬度和经度
- 21. 将东北坐标转换为正常的经度和纬度
- 22. 计算给定经度和纬度的地球凸包多边形区域
- 23. 基于经度和纬度计算最近位置的算法
- 24. PHP转义经纬度和长坐标。
- 25. 将坐标分为纬线和纬度
- 26. 将x,y,区域坐标转换为经度和纬度
- 27. 给出了经纬度点,找到边缘点和多边形区域
- 28. C#的纬度和经度边界框?
- 29. 在X,Y坐标中翻译经度和纬度
- 30. Swift:获取用户的位置坐标作为经度和纬度?
如何 “短” 是足够短?几米? KM? – 2010-09-28 04:54:36
@Paul Alexander - 你需要多准确?地球的周长大约是40000公里,所以如果你谈论的是1000公里,那么经历的地球曲率将不到1度。我会说这相当准确,但取决于您的应用程序。 – 2010-09-28 05:24:13