加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中的Google几何Api

发布时间:2020-12-15 22:51:51 所属栏目:百科 来源:网络整理
导读:我有一个点(纬度,经度)ex:33.959295,35.606100我正在寻找一种方法在c#中检查这个点是否在特定路线(点列表或折线).我做了一些研究,我发现谷歌地图几何图书馆中包含的is??LocationOnEdge功能正是我所需要的,但它不适用于c#.以下是其他语言的一些示例: Google
我有一个点(纬度,经度)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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读