我有一个点(纬度,经度)ex:33.959295,35.606100我正在寻找一种方法在c#中检查这个点是否在特定路线(点列表或折线).我做了一些研究,我发现谷歌地图几何图书馆中包含的isLocationOnEdge功能正是我所需要的,但它不适用于c#.以下是其他语言的一些示例:
> Google Map Javascript API https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge.
> Android示例https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java
>我为gmaps google maps API for C#找到了一个c#库,但它不支持isLocationOnEdge函数
有没有办法在c#中执行上面的要求?
解决方法
以下是IsLocationOnEdge For C#的实现.
using System; using System.Collections.Generic; using System.Device.Location; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; namespace TestConsoleApp { class Program { static void Main(string[] args) { var path = new List<Location> { new Location(1,1),new Location(2,2),new Location(3,3),}; var point = new Location(1.9,1.5); bool isOnEdge = isLocationOnEdge(path,point); Console.ReadKey(); } static bool isLocationOnEdge(List<Location> path,Location point,int tolerance = 2) { var C = new GeoCoordinate(point.Lat,point.Lng); for (int i = 0; i < path.Count - 1; i++) { var A = new GeoCoordinate(path[i].Lat,path[i].Lng); var B = new GeoCoordinate(path[i + 1].Lat,path[i + 1].Lng); if (Math.Round(A.Getdistanceto(C) + B.Getdistanceto(C),tolerance) == Math.Round(A.Getdistanceto(B),tolerance)) { return true; } } return false; } } class Location { public Location(double Lat,double Lng) { this.Lat = Lat; this.Lng = Lng; } public double Lat { get; set; } public double Lng { get; set; } } }
参考文献:
Check is a point (x,y) is between two points drawn on a straight line
Calculating Distance between two Latitude and Longitude GeoCoordinates
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。