【问题标题】:google Geometry Library in C# [duplicate]C#中的谷歌几何库[重复]
【发布时间】:2019-04-24 14:16:06
【问题描述】:

我有一个点(纬度,经度)例如:25,-80,我正在寻找一种方法在 c# 中检查该点是否在特定的多边形中。

我做了一些研究,发现 Google 地图几何库中包含的 containsLocation 函数正是我需要的,但它不适用于 c#。这是一个在 JS 中使用该方法的示例:

// This example requires the Geometry library. Include the libraries=geometry
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry">

function initMap() {

        var triangleCoords = [
          {lat: 25.774, lng: -80.19},
          {lat: 18.466, lng: -66.118},
          {lat: 32.321, lng: -64.757}
        ];

        var coords = new google.maps.LatLng(25.774, -80.19);
        var bermudaTriangle = new google.maps.Polygon({paths: triangleCoords});

        var result = google.maps.geometry.poly.containsLocation(coords, bermudaTriangle);
        alert('The location exist in polygon: '+result);
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap"
         async defer></script>

我找到了一个用于 gmaps google maps API for C# 的 c# 库,但它不支持函数 containsLocation 有没有办法在 c# 中完成上述要求?

【问题讨论】:

    标签: javascript c# google-maps google-poly


    【解决方案1】:

    Ray casting algorithm 通常用于确定给定点是否位于多边形内。 This answer 包含 C# 中的实现。

    也可以使用NetTopologySuite library,特别是NetTopologySuite.Algorithm.Locate.IndexedPointInAreaLocator class

    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");
    }
    

    【讨论】:

    • 不如 google map API 提供的 containslocation 方法准确。在某些位置,它在 CreatePolygon 方法中给出异常“点必须形成闭合线串”,而 GMAP API 对这些坐标工作正常。 @VadimGremyachev
    • 例如在 var triangleCoords = new[] { new Coordinate(25.774, -80.19), new Coordinate(18.466, -66.118), new Coordinate(32.321, -64.757), new Coordinate(32.321, - 62.757) }; checkLatLong = {20.8, -69.5}
    • @devedv,这个异常与“精度”无关,因为多边形的起点和终点必须相等
    • 是的,它适用于提供的示例,但在我的情况下,我总是没有准确的多边形。你能为此提出一些建议吗?谢谢。
    • 一个选项是确保坐标数组中的第一项和最后一项相同:假设您有:var triangleCoords = new[] { new Coordinate(25.774, -80.19),...} 然后考虑像这样附加最后一项:triangleCoords = new List&lt;string&gt;(triangleCoords).Add(triangleCoords[0]).ToArray();
    猜你喜欢
    • 2018-12-01
    • 2016-09-11
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多