Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google geometry Api in 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/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

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

like image 306
User7291 Avatar asked Jun 22 '18 13:06

User7291


People also ask

Can I use Google Maps API for free?

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).

Is Google static map API free?

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.

How do I get data from Google Maps API?

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.


1 Answers

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

like image 174
Mihir Dave Avatar answered Sep 29 '22 04:09

Mihir Dave