6

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:

Is there a way to do the required above in c#?

User7291
  • 1,015
  • 3
  • 24
  • 65
  • Two options come to mind: 1) you might be able run the required Javascript in C# using the V8 JavaScript engine, like in [this library](https://github.com/JavascriptNet/Javascript.Net) 2) set up a web site that makes the call for you and returns the results. – stuartd Jun 22 '18 at 13:34

1 Answers1

3

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

Mihir Dave
  • 3,869
  • 1
  • 10
  • 26