C#中的Google几何Api [英] Google geometry Api in C#

查看:105
本文介绍了C#中的Google几何Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个点(纬度,经度),例如:33.959295,35.606100,我在c#中寻找一种方法来检查该点是否在特定路线上(点列表或折线).我进行了一些研究,发现Google Maps Geometry Library中包含的isLocationOnEdge函数完全可以满足我的需要,但不适用于c#.这是其他语言的一些示例:

I have a point (latitude,longitude) ex : 33.959295,35.606100 and I'm looking for a way in c# to check if this point is on a specific route (a list of points or a polyline). I did some research and I found that isLocationOnEdge function contained within the Google Maps Geometry Library does exactly what I need but it is not available for c#. Here are some examples in other languages:

  • Google Map Javascript API https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge.
  • Android sample https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java
  • I found a c# library for gmaps google maps API for C# but it does not support the function isLocationOnEdge

有没有一种方法可以在c#中完成上述要求?

Is there a way to do the required above in c#?

推荐答案

这是IsLocationOnEdge For C#的实现.

Here is the implementation for 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; }
    }
}

参考:

检查是点(x,y)在直线上画出的两个点之间 计算两个纬度和经度地理坐标之间的距离

这篇关于C#中的Google几何Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆