Ejemplo n.º 1
0
        //Overload. Makes route between all the stops and back to first stop, for the main mage.
        public async Task SetGoogleRoute(List <stop> stops)
        {
            if (stops.Count > 2)
            {
                //duration = "n/a";
                //distance = "n/a";
                //mode = "n/a";

                //GoogleDirections _
                googleRoute = new GoogleDirections();
                // HttpClient client = new HttpClient();
                string firstPoint = stops[0].stopLat.ToString() + "," + stops[0].stopLng.ToString();
                //string lastPoint = stops.Last().stopLat.ToString() + "," + stops.Last().stopLng.ToString();
                string waypoints = "&waypoints=";

                foreach (stop stop in stops.Skip(1))
                {
                    waypoints += "via:" + stop.stopLat.ToString() + "," + stop.stopLng.ToString() + "|";
                }

                //--Inside a try, becuase it was chrashing if it coudln't get to internet to resolve hostname.
                //  Which happens more than you'd think in the wild ... phone connects to wifi with a captive portal or loses signal for a moment, ect.
                //  It doesn't matter if this fails, we just don't get to draw a pretty route line on the map.
                try
                {
                    HttpResponseMessage response = await client.GetAsync("https://api.patnet.net/?origin=" + firstPoint + "&destination=" + firstPoint + waypoints.Trim('|') + "&mode=walking");

                    if (response.IsSuccessStatusCode)
                    {
                        // Get the response
                        var jsonString = response.Content.ReadAsStringAsync().Result;

                        // Deserialise the data (include the Newtonsoft JSON Nuget package if you don't already have it)
                        googleRoute = JsonConvert.DeserializeObject <GoogleDirections>(jsonString);

                        //ZERO_RESULTS happens when it can't find a route, like if someon needs to drive across the ocean.
                        if (googleRoute.status != "ZERO_RESULTS")
                        {
                            duration = googleRoute.routes.FirstOrDefault().legs.FirstOrDefault().duration.text ?? "n/a";
                            distance = googleRoute.routes.FirstOrDefault().legs.FirstOrDefault().distance.text ?? "n/a";
                            mode     = "walking";
                        }
                    }
                }
                catch { }
            }

            //If 2 points or less, we can use the other "set google route"
            else if (stops.Count == 2)
            {
                await SetGoogleRoute(stops[0].stopLat.ToString() + "," + stops[0].stopLng.ToString(), stops[1].stopLat.ToString() + "," + stops[1].stopLng.ToString());
            }

            else if (stops.Count == 1)
            {
                await SetGoogleRoute(stops[0].stopLat.ToString() + "," + stops[0].stopLng.ToString(), stops[0].stopLat.ToString() + "," + stops[0].stopLng.ToString());
            }

            //client.CancelPendingRequests();
        }
Ejemplo n.º 2
0
 public googleHelper()
 {
     googleRoute        = new GoogleDirections();
     polylinePointsList = new List <Position>();
     distance           = string.Empty;
     duration           = string.Empty;
     client             = new HttpClient();
 }
Ejemplo n.º 3
0
        //By calling to our own API instead of to google directly, we gain several advantages. First, we hide our API key to prevent theft.
        //Second, we can capture user data if we wanted to do something with it. Say, we see one user is making a ridiculous number of calls, we can throttle (or block) that user.
        //Or if we wanted to add advertisments, we could do it based on location gathered from start pos, or for things near endpos. Ect.
        //All the api does for now is return the route without any of that kind of logic, but by having the api in place, that sort of stuff can be added later.
        public async Task SetGoogleRoute(string startPos, string endPos, string mode = "walking")
        {
            googleRoute = new GoogleDirections();

            timestamp = DateTimeOffset.Now.UtcDateTime;

            //--Inside a try, becuase it was chrashing if it coudln't get to internet to resolve hostname
            //  Which happens more than you'd think in the wild ... phone connects to wifi with a captive portal or loses signal for a moment, ect.
            //  It doesn't matter if this fails, we just don't get to draw a pretty route line on the map.
            try
            {
                HttpResponseMessage response = await client.GetAsync("https://api.patnet.net/?origin=" + startPos + "&destination=" + endPos + "&mode=" + mode + "&guid=" + Preferences.Get("guid", "guid_unset"));

                if (response.IsSuccessStatusCode)
                {
                    // Get the response
                    var jsonString = response.Content.ReadAsStringAsync().Result;

                    // Deserialise the data (include the Newtonsoft JSON Nuget package if you don't already have it)
                    googleRoute = JsonConvert.DeserializeObject <GoogleDirections>(jsonString);

                    //ZERO_RESULTS happens when it can't find a route, like if someon needs to drive across the ocean.
                    if (googleRoute.status != "ZERO_RESULTS")
                    {
                        duration  = googleRoute.routes.FirstOrDefault().legs.FirstOrDefault().duration.text ?? "n/a";
                        distance  = googleRoute.routes.FirstOrDefault().legs.FirstOrDefault().distance.text ?? "n/a";
                        this.mode = mode;
                    }
                    else
                    {
                        duration  = "n/a";
                        distance  = "n/a";
                        this.mode = "n/a";
                    }
                }
            }
            catch {
                duration  = "n/a";
                distance  = "n/a";
                this.mode = "n/a";
            }

            client.CancelPendingRequests();
        }
Ejemplo n.º 4
0
        //To get a detailed line that lays on top of roads accurately, we need to get the enocded line points from each "step" that google returns.
        //This combines all those into one decoded line
        public List <Position> detailedPolyline(GoogleDirections fullRoute)
        {
            List <Position> returnMe = new List <Position>();

            if (fullRoute.status != "ZERO_RESULTS" & fullRoute.routes != null)
            {
                foreach (Step step in fullRoute.routes[0].legs[0].steps.ToList())
                {
                    foreach (Position position in FnDecodePolylinePoints(step.polyline.points))
                    {
                        returnMe.Add(position);
                    }
                }
            }



            return(returnMe);
        }