我想检查一个位置是否在 C# 中的某个区域内,但谷歌库仅在 JavaScript 中可用 示例代码
var triangleCoords = [
{lat: 13.1356661, lng: 77.4975465},
{lat: 13.1156035, lng: 77.4082768},
{lat: 13.0085758, lng: 77.3808094},
{lat: 12.8713722, lng: 77.3931875},
{lat: 12.8385751, lng: 77.4714526},
{lat: 12.6805186, lng: 77.7337679},
{lat: 12.7247306, lng: 77.9260408},
{lat: 12.8050967, lng: 77.9411481},
{lat: 12.9416603, lng: 77.8683592},
{lat: 13.0848379, lng: 77.8532521},
{lat: 13.2413016, lng: 77.7887033},
{lat: 13.3001168, lng: 77.7351415},
{lat: 13.2680376, lng: 77.6774595},
{lat: 13.159739, lng: 77.5662156},
{lat: 13.1356661, lng: 77.4975465}
];
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.2,
strokeWeight: 2,
fillColor: '#00ff00',
fillOpacity: 0
});
bermudaTriangle.setMap(map);
google.maps.event.addListener(map, 'click', function(e) {
var resultColor =
google.maps.geometry.poly.containsLocation(e.latLng, bermudaTriangle) ?
'black' :
'green';
if(google.maps.geometry.poly.containsLocation(e.latLng, bermudaTriangle)) {
alert('inside the city');
}
链接到工作示例
我对加载地图不感兴趣,我只是希望能够进行检查
NetTopologySuite
库可用于此目的,下面是一个如何确定多边形中给定点的示例:
var triangleCoords = new[] {
new Coordinate(25.774, -80.19),
new Coordinate(18.466, -66.118),
new Coordinate(32.321, -64.757),
new Coordinate(25.774, -80.19)
};
IGeometryFactory geometryFactory = new GeometryFactory();
var poly = geometryFactory.CreatePolygon(triangleCoords);
var locator = new NetTopologySuite.Algorithm.Locate.IndexedPointInAreaLocator(poly);
var location = locator.Locate(new Coordinate(24.886, -70.269));
if (location == GeoAPI.Geometries.Location.Interior)
{
Console.WriteLine("Polygon contains the location");
}
如果您不希望依赖其他库,则此答案包含光线投射算法的 C# 实现,该算法通常用于查找点是在多边形内部还是外部。