async Task <bool> CalculateRoute()
        {
            buttonGrid.IsVisible = false;
            map.Polylines.Clear();
            RouteReset();
            var line1 = new Xamarin.Forms.GoogleMaps.Polyline();
            var line2 = new Xamarin.Forms.GoogleMaps.Polyline();

            fastest              = new Xamarin.Forms.GoogleMaps.Polyline();
            cleanest             = new Xamarin.Forms.GoogleMaps.Polyline();
            fastest.StrokeColor  = Color.Blue;
            fastest.StrokeWidth  = 7;
            cleanest.StrokeColor = Color.FromHex("00e36f");
            cleanest.StrokeWidth = 4;

            line1.StrokeColor = Color.Blue;
            line1.StrokeWidth = 7;
            line2.StrokeColor = Color.FromHex("00e36f");
            line2.StrokeWidth = 4;

            string originParam      = originPos.Latitude.ToString() + ',' + originPos.Longitude.ToString();
            string destinationParam = destinationPos.Latitude.ToString() + ',' + destinationPos.Longitude.ToString();

            line1.Positions.Clear();
            line2.Positions.Clear();
            fastest.Positions.Clear();
            cleanest.Positions.Clear();

            maxTime          = 0;
            fastestDistance  = 0;
            cleanestDistance = 0;
            fastestPollution = 0;
            cleanestTime     = 0;
            maxPollution     = 0;

            if (!string.IsNullOrEmpty(originParam) && !string.IsNullOrEmpty(destinationParam) && isHomeSet > 0 && isDestinationSet > 0)
            {
                Bounds bounds = new Bounds(originPos, destinationPos);
                map.MoveToRegion(MapSpan.FromBounds(bounds));
                blueDistanceLabel.Text    = "";
                magentaDistanceLabel.Text = "";

                await GetHeatMap(bounds);

                var mqResult = await rest.GetMQAlternativeDirection(originParam, destinationParam, mqModes[mode]);

                List <Position> pollutionPoints = new List <Position>();

                bool isRouteChosen = false;
                if (mqResult != null)
                {
                    if (mqResult.route != null)
                    {
                        if (mqResult.route.shape != null)
                        {
                            fastest.Positions.Clear();
                            cleanest.Positions.Clear();
                            if (!string.IsNullOrEmpty(mqResult.route.shape.shapePoints))
                            {
                                var points = DecodePolyline(mqResult.route.shape.shapePoints);
                                foreach (var point in points)
                                {
                                    fastest.Positions.Add(point);
                                    cleanest.Positions.Add(point);
                                    pollutionPoints.Add(point);
                                }
                                maxPollution = await CalculatePollution(pollutionPoints, true);

                                fastestPollution = maxPollution;
                                maxTime          = mqResult.route.time;
                                cleanestTime     = mqResult.route.time;
                                fastestDistance  = mqResult.route.distance;
                                cleanestDistance = mqResult.route.distance;
                            }
                        }
                    }
                    MQDirection fastestRoute  = new MQDirection();
                    MQDirection cleanestRoute = new MQDirection();

                    if (mqResult.route.alternateRoutes != null)
                    {
                        if (mqResult.route.alternateRoutes.Count > 0)
                        {
                            bool duplicated = false;
                            map.Polylines.Clear();
                            fastestRoute  = mqResult.route.alternateRoutes[0];
                            cleanestRoute = mqResult.route.alternateRoutes[0];
                            foreach (var item in mqResult.route.alternateRoutes)
                            {
                                line1.Positions.Clear();
                                line2.Positions.Clear();
                                pollutionPoints.Clear();
                                if (item.route.shape != null)
                                {
                                    if (!string.IsNullOrEmpty(item.route.shape.shapePoints))
                                    {
                                        var points = DecodePolyline(item.route.shape.shapePoints);
                                        foreach (var point in points)
                                        {
                                            line1.Positions.Add(point);
                                            line2.Positions.Add(point);
                                            pollutionPoints.Add(point);
                                            if (point.Latitude.Equals(hotspot.Latitude) && point.Longitude.Equals(hotspot.Longitude))
                                            {
                                                duplicated = true;
                                                break;
                                            }
                                        }
                                        if (!duplicated)
                                        {
                                            float pollutionValue = await CalculatePollution(pollutionPoints, true);

                                            if (item.route.time < maxTime)
                                            {
                                                fastest          = line1;
                                                fastestRoute     = item;
                                                maxTime          = item.route.time;
                                                fastestPollution = pollutionValue;
                                                fastestDistance  = item.route.distance;
                                                isRouteChosen    = true;
                                            }
                                            else if (pollutionValue < maxPollution)
                                            {
                                                cleanest         = line2;
                                                cleanestRoute    = item;
                                                maxPollution     = pollutionValue;
                                                cleanestTime     = item.route.time;
                                                cleanestDistance = item.route.distance;
                                                isRouteChosen    = true;
                                            }
                                        }
                                    }
                                }
                            }
                            if (isRouteChosen && fastestPollution > maxPollution)
                            {
                                map.Polylines.Clear();

                                if (fastest.Positions.Count >= 2)
                                {
                                    map.Polylines.Add(fastest);
                                }
                                if (cleanest.Positions.Count >= 2)
                                {
                                    map.Polylines.Add(cleanest);
                                }

                                blueDistanceLabel.Text    = $"{fastestDistance.ToString("F1")} miles {timeToMin(maxTime)} Pollution: {(int)fastestPollution}";
                                magentaDistanceLabel.Text = $"{cleanestDistance.ToString("F1")} miles {timeToMin(cleanestTime)} Pollution: {(int)maxPollution}";
                                buttonGrid.IsVisible      = true;
                                drawHotspot();
                                return(true);
                            }
                        }
                    }

                    buttonGrid.IsVisible = false;
                    map.Polylines.Clear();
                    Device.StartTimer(TimeSpan.FromMilliseconds(3000), () =>
                    {
                        if (fastest.Positions.Count >= 2)
                        {
                            fastest.StrokeColor = Color.FromHex("00e36f");
                            map.Polylines.Add(fastest);
                        }

                        magentaDistanceLabel.Text         = $"{mqResult.route.distance.ToString("F1")} miles {timeToMin(mqResult.route.time)} Pollution: {(int)fastestPollution}";
                        blueDistanceLabel.BackgroundColor = Color.Gray;
                        blueDistanceLabel.Text            = "No faster route available.";
                        drawHotspot();
                        return(false);
                    });
                }
            }
            else
            {
                await DisplayAlert("Warning", "Please fill all the fields", "OK");
            }
            return(false);
        }
        async Task <bool> CalculateRoute()
        {
            map.Polylines.Clear();
            var line1 = new Xamarin.Forms.GoogleMaps.Polyline();

            line1.StrokeColor = Color.Blue;
            line1.StrokeWidth = 7;
            var line2 = new Xamarin.Forms.GoogleMaps.Polyline();

            line2.StrokeColor = Color.Magenta;
            line2.StrokeWidth = 4;

            string originParam      = originPos.Latitude.ToString() + ',' + originPos.Longitude.ToString();
            string destinationParam = destinationPos.Latitude.ToString() + ',' + destinationPos.Longitude.ToString();

            if (isHomeSet == 1)
            {
                originParam = origin;
            }

            if (isDestinationSet == 1)
            {
                destinationParam = destination;
            }

            Bounds bounds = new Bounds(originPos, destinationPos);

            map.MoveToRegion(MapSpan.FromBounds(bounds));
            if (!string.IsNullOrEmpty(originParam) && !string.IsNullOrEmpty(destinationParam) && isHomeSet > 0 && isDestinationSet > 0)
            {
                //var distanceResult = await rest.GetDistance(originParam, destinationParam, modes[mode]);
                //if (distanceResult != null)
                //{
                //    if (distanceResult.rows[0].elements[0].distance != null)
                //    {
                //        string distance = distanceResult.rows[0].elements[0].distance.text;
                //        string duration = distanceResult.rows[0].elements[0].duration.text;
                //        distanceLabel.Text = $"Red Distance={distance}, Duration={duration}";
                //    }
                //}

                //var result = await rest.GetDirection(originParam, destinationParam, modes[mode]);

                //if (result != null)
                //{
                //    var line = new Xamarin.Forms.GoogleMaps.Polyline();
                //    line.StrokeColor = Color.Red;
                //    line.StrokeWidth = 10;
                //    foreach (var route in result.routes)
                //    {
                //        foreach (var leg in route.legs)
                //        {
                //            foreach (var step in leg.steps)
                //            {
                //                var points = DecodePolyline(step.polyline.points);
                //                foreach (var point in points)
                //                {
                //                    line.Positions.Add(point);
                //                }
                //            }
                //        }
                //    }
                //    if (line.Positions.Count >= 2)
                //        map.Polylines.Add(line);
                //}

                var mqResult = await rest.GetMQAlternativeDirection(originParam, destinationParam, mqModes[mode]);

                List <Position> pollutionPoints = new List <Position>();

                if (mqResult != null)
                {
                    if (mqResult.route != null)
                    {
                        if (mqResult.route.shape != null)
                        {
                            if (!string.IsNullOrEmpty(mqResult.route.shape.shapePoints))
                            {
                                var points = DecodePolyline(mqResult.route.shape.shapePoints);
                                foreach (var point in points)
                                {
                                    line1.Positions.Add(point);
                                    pollutionPoints.Add(point);
                                }

                                if (line1.Positions.Count >= 2)
                                {
                                    map.Polylines.Add(line1);
                                }
                                maxPollution = await CalculatePollution(pollutionPoints, true);

                                drawHotspot();
                            }
                        }
                    }

                    if (mqResult.route.alternateRoutes != null)
                    {
                        bool duplicated = false;
                        foreach (var item in mqResult.route.alternateRoutes)
                        {
                            line2.Positions.Clear();
                            pollutionPoints.Clear();
                            if (item.route.shape != null)
                            {
                                if (!string.IsNullOrEmpty(item.route.shape.shapePoints))
                                {
                                    var points = DecodePolyline(item.route.shape.shapePoints);
                                    foreach (var point in points)
                                    {
                                        line2.Positions.Add(point);
                                        pollutionPoints.Add(point);
                                        if (point.Latitude.Equals(hotspot.Latitude) && point.Longitude.Equals(hotspot.Longitude))
                                        {
                                            duplicated = true;
                                            break;
                                        }
                                    }
                                    if (!duplicated)
                                    {
                                        float pollutionValue = await CalculatePollution(pollutionPoints, true);

                                        if (pollutionValue > maxPollution)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            if (line2.Positions.Count >= 2)
                                            {
                                                map.Polylines.Add(line2);
                                            }
                                            drawHotspot();
                                            return(true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                await this.DisplayAlert("Warning", "Please fill all the fields", "OK");
            }
            return(false);
        }