Use*_*291 5 c# google-maps google-polyline google-geolocation
我有一个点(纬度,经度),例如:33.959295,35.606100,我正在c#中寻找一种方法来检查该点是否在特定路线上(点列表或折线)。我做了一些研究,发现其中isLocationOnEdge包含的功能Google Maps Geometry Library确实满足我的需要,但不适用于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; }
}
}
Run Code Online (Sandbox Code Playgroud)
参考:
检查点 (x,y) 是否位于直线上绘制的两点之间 计算两个纬度和经度地理坐标之间的距离
| 归档时间: |
|
| 查看次数: |
869 次 |
| 最近记录: |