Exemple #1
0
        public bool Contains(GeofenceItem geofence, Location point)
        {
            //Credits: https://stackoverflow.com/a/7739297/2313836

            var c = false;

            for (int i = 0, j = geofence.Polygons.Count - 1; i < geofence.Polygons.Count; j = i++)
            {
                if ((((geofence.Polygons[i].Latitude <= point.Latitude) && (point.Latitude < geofence.Polygons[j].Latitude)) ||
                     ((geofence.Polygons[j].Latitude <= point.Latitude) && (point.Latitude < geofence.Polygons[i].Latitude))) &&
                    (point.Longitude < (geofence.Polygons[j].Longitude - geofence.Polygons[i].Longitude) * (point.Latitude - geofence.Polygons[i].Latitude)
                     / (geofence.Polygons[j].Latitude - geofence.Polygons[i].Latitude) + geofence.Polygons[i].Longitude))
                {
                    c = !c;
                }
            }
            return(c);
        }
Exemple #2
0
        public static GeofenceItem FromFile(string filePath)
        {
            var geofence = new GeofenceItem();
            var lines    = File.ReadAllLines(filePath);

            foreach (var line in lines)
            {
                if (line.StartsWith("[", StringComparison.Ordinal))
                {
                    geofence.Name = line.TrimStart('[').TrimEnd(']');
                    continue;
                }

                var coordinates = line.Replace(" ", null).Split(',');
                var lat         = double.Parse(coordinates[0]);
                var lng         = double.Parse(coordinates[1]);

                geofence.Polygons.Add(new Location(lat, lng));
            }

            return(geofence);
        }