C#中的Google几何Api [英] Google geometry Api in C#
问题描述
我有一个点(纬度,经度),例如: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/文档/javascript/geometry#isLocationOnEdge .
- Android示例适用于C#的Google Maps API ,但是它不支持功能
isLocationOnEdge
- 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屋!