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:
isLocationOnEdge
Is there a way to do the required above in c#?
Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).
The Maps Static API uses a pay-as-you-go pricing model. Requests for the Maps Static API are billed under the SKU for Static Maps. Along with the overall Google Terms of Use, there are usage limits specific to the Maps Static API. Manage your costs and usage with tools available in the Google Cloud Console.
Go to APIs & Services → Dashboard → Enable APIs & Services at the top and Choose Maps Javascript API from the API Library. This will open up the Map JavaScript API page, and Enable it.
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; }
}
}
References:
Check is a point (x,y) is between two points drawn on a straight line Calculating Distance between two Latitude and Longitude GeoCoordinates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With