简体   繁体   中英

google Geometry Library in C#

I have a point (latitude,longitude) ex : 25,-80 and I'm looking for a way in c# to check if this point is in specific polygon.

I did some research and I found that containsLocation function contained within the Google Maps Geometry Library does exactly what I need but it is not available for c#. Here is an examples in which this method is utlized in 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> 

I found ac# library for gmaps google maps API for C# but it does not support the function containsLocation Is there a way to do the required above in c#?

Ray casting algorithm is commonly used to determine whether the given point is located within polygon. This answer contains the implementation in C#.

Also NetTopologySuite library could be utilized for that matter, in particular 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM