C#中的Google几何Api

我有一个点(纬度,经度)ex:33.959295,35.606100我正在寻找一种方法在c#中检查这个点是否在特定路线(点列表或折线).我做了一些研究,我发现谷歌地图几何图书馆中包含的is​​LocationOnEdge功能正是我所需要的,但它不适用于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

点赞