c# - C# 中的谷歌几何 API
问题描述
我有一个点(纬度,经度),例如:33.959295,35.606100,我正在 C# 中寻找一种方法来检查该点是否在特定路线上(点列表或折线)。我做了一些研究,发现其中isLocationOnEdge
包含的函数Google Maps Geometry Library
正是我需要的,但它不适用于 c#。以下是其他语言的一些示例:
- 谷歌地图 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
- 我找到了 ac# library for gmaps google maps API for 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; }
}
}
参考:
推荐阅读
- html - 如何修复选择primeng?
- java - Javamail:适用于“Outlook.com”电子邮件,但不适用于也使用 Outlook 的“mycompany.com”电子邮件
- javascript - 如何使用 Express、Vue.js 和 firebase 部署客户端/服务器网站
- julia - 在 Julia 的模块内调用的基准测试函数
- jupyter-notebook - 在一台电脑上成功运行代码但在另一台电脑上失败,即使使用相同的用户和相同的文件访问相同的远程服务器
- android - 我试图在房间数据库中添加数据但无法添加超过 50 行
- javascript - 显示图标
角度组件(primeng-table) - javascript - maplibregl 未在生产中显示
- c# - c#如何在一行中获取多个输入
- amazon-web-services - 为 S3 上的动态分区文件夹结构生成 Athena 表