【问题标题】:Google geometry Api in C#C# 中的谷歌几何 API
【发布时间】:2018-12-01 23:20:36
【问题描述】:

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

有没有办法在 c# 中完成上述要求?

【问题讨论】:

  • 我想到了两个选项:1) 您可以使用 V8 JavaScript 引擎在 C# 中运行所需的 Javascript,就像在 this library 中一样 2) 建立一个为您拨打电话的网站并返回结果。

标签: c# google-maps google-polyline google-geolocation


【解决方案1】:

这里是 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2013-08-16
    • 2012-01-24
    相关资源
    最近更新 更多