private List<string> handleDirectionsRoute(GDirections direction, List<PointLatLng> newPoints)
        {
            double jumpDistance = 0.0001 * sliderJumpLength.Value;

            List<PointLatLng> route = direction.Route;

            for (int j = 1; j < route.Count; j++)
            {
                PointLatLng pointFirst = route[j - 1];
                PointLatLng pointLast = route[j];

                double dist = distanceBetweenPoints(pointFirst, pointLast);

                int numOfInterpolated = (int)(Math.Round(dist / jumpDistance) - 1);

                newPoints.Add(pointFirst);

                if (numOfInterpolated < 1)
                    continue;

                for (int k = 1; k <= numOfInterpolated; k++)
                {
                    double latitude = pointFirst.Lat * (numOfInterpolated - k) / numOfInterpolated
                        + pointLast.Lat * k / numOfInterpolated;
                    double longitude = pointFirst.Lng * (numOfInterpolated - k) / numOfInterpolated
                        + pointLast.Lng * k / numOfInterpolated;

                    newPoints.Add(new PointLatLng(latitude, longitude));
                }
            }

            Console.WriteLine("Number of points: " + newPoints.Count);
            if (newPoints.Count == 0)
                return null;

            List<string> listPanoIds = new List<string>();
            parsePoints(0, listPanoIds, newPoints, "");
            return listPanoIds;
        }
        // adds route
        private void buttonRouteAdd_Click(object sender, RoutedEventArgs e)
        {
            DirectionsProvider dp = MainMap.MapProvider as DirectionsProvider;

            bool avoidHighways = (bool)checkBoxAvoidHighways.IsChecked;
            bool avoidTools = (bool)checkBoxAvoidTools.IsChecked;
            bool walkingMode = (bool)checkBoxWalkingMode.IsChecked;



            GDirections direction = new GDirections();
            DirectionsStatusCode status = dp.GetDirections
                (out direction, start, end, avoidHighways, avoidTools, walkingMode, true, true);
            //Debug.WriteLine(status);
            //Debug.WriteLine(direction.Route.ToString());
            if (direction != null && status == DirectionsStatusCode.OK)
            {
                GMapMarker m1 = new GMapMarker(start);
                m1.Shape = new CustomMarkerDemo(this, m1, "Start: " + direction.StartAddress);

                GMapMarker m2 = new GMapMarker(end);
                m2.Shape = new CustomMarkerDemo(this, m2, "End: " + direction.EndAddress);

                GMapRoute mRoute = new GMapRoute(direction.Route);
                {
                    mRoute.ZIndex = -1;
                }
                MainMap.Markers.Add(m1);
                MainMap.Markers.Add(m2);
                MainMap.Markers.Add(mRoute);

                MainMap.ZoomAndCenterMarkers(null);

                MainMap.hasDirection = true;
                MainMap.selectedDirection = direction;


                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                /*GoogleMapProvider gp = GMapProviders.GoogleMap;
                GeoCoderStatusCode StreetViewStatus = new GeoCoderStatusCode();

                string panoId = gp.GetPanoId(direction.StartLocation, out StreetViewStatus);
                Debug.WriteLine("PanoId: "+panoId);*/
            }
            else
            {
                MainMap.hasDirection = false;
                MainMap.selectedDirection = null;
            }

            /*RoutingProvider rp = MainMap.MapProvider as RoutingProvider;
            Debug.WriteLine(rp);
            if (rp == null)
            {
                //rp = GMapProviders.GoogleMap; // use OpenStreetMap if provider does not implement routing
            }

            MapRoute route = rp.GetRoute(start, end, false, false, (int)MainMap.Zoom);
            if (route != null)
            {
                GMapMarker m1 = new GMapMarker(start);
                m1.Shape = new CustomMarkerDemo(this, m1, "Start: " + route.Name);

                GMapMarker m2 = new GMapMarker(end);
                m2.Shape = new CustomMarkerDemo(this, m2, "End: " + start.ToString());

                GMapRoute mRoute = new GMapRoute(route.Points);
                {
                    mRoute.ZIndex = -1;
                }

                MainMap.Markers.Add(m1);
                MainMap.Markers.Add(m2);
                MainMap.Markers.Add(mRoute);

                MainMap.ZoomAndCenterMarkers(null);
            }*/
        }
        private void buttonStreetView_Click(object sender, RoutedEventArgs e)
        {
            DirectionsProvider dp = MainMap.MapProvider as DirectionsProvider;

            bool avoidHighways = (bool)checkBoxAvoidHighways.IsChecked;
            bool avoidTools = (bool)checkBoxAvoidTools.IsChecked;
            bool walkingMode = (bool)checkBoxWalkingMode.IsChecked;



            GDirections direction = new GDirections();
            DirectionsStatusCode status = dp.GetDirections
                (out direction, start, end, avoidHighways, avoidTools, walkingMode, true, true);
            //Debug.WriteLine(status);
            //Debug.WriteLine(direction.Route.ToString());


            if (direction != null && status == DirectionsStatusCode.OK)
            {
                GMapMarker m1 = new GMapMarker(start);
                m1.Shape = new CustomMarkerDemo(this, m1, "Start: " + direction.StartAddress);

                GMapMarker m2 = new GMapMarker(end);
                m2.Shape = new CustomMarkerDemo(this, m2, "End: " + direction.EndAddress);

                GMapRoute mRoute = new GMapRoute(direction.Route);
                {
                    mRoute.ZIndex = -1;
                }
                MainMap.Markers.Add(m1);
                MainMap.Markers.Add(m2);
                MainMap.Markers.Add(mRoute);

                MainMap.ZoomAndCenterMarkers(null);

                MainMap.hasDirection = true;
                MainMap.selectedDirection = direction;

                List<PointLatLng> newPoints = new List<PointLatLng>();
                List<string> listPanoIds = handleDirectionsRoute(direction, newPoints);

                //Debug.WriteLine("Pano Ids list: " + String.Join("\n", listPanoIds));
                Debug.WriteLine("Number of PanoIds: " + listPanoIds.Count);

                if (listPanoIds == null || listPanoIds.Count == 0)
                {
                    return;
                    /*GoogleMapProvider gp = GMapProviders.GoogleMap;
                    GeoCoderStatusCode StreetViewStatus = new GeoCoderStatusCode();
                    //List<string> listPanoIds = new List<string>();
                    listPanoIds = new List<string>();
                    
                    string panoId = gp.GetPanoId(start, out StreetViewStatus);
                    if (panoId != null && !panoId.Equals(String.Empty))
                        listPanoIds.Add(panoId);
                    else
                        return;*/
                }



                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();


                if ((sender as Button).Name.Equals("buttonStreetViewQuality"))
                {
                    startStreetView(listPanoIds, 3, false);
                }
                else if ((sender as Button).Name.Equals("buttonStreetViewQuick"))
                {
                    startStreetView(listPanoIds, 2, false);
                }
                else if ((sender as Button).Name.Equals("buttonOculusView"))
                {
                    startStreetView(listPanoIds, 3, true);
                }
            }
            else
            {
                MainMap.hasDirection = false;
                MainMap.selectedDirection = null;

                GoogleMapProvider gp = GMapProviders.GoogleMap;
                GeoCoderStatusCode StreetViewStatus = new GeoCoderStatusCode();
                //List<string> listPanoIds = new List<string>();
                List<string> listPanoIds = new List<string>();
                    
                string panoId = gp.GetPanoId(start, out StreetViewStatus);
                if (panoId != null && !panoId.Equals(String.Empty))
                {
                    listPanoIds.Add(panoId);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();


                    if ((sender as Button).Name.Equals("buttonStreetViewQuality"))
                    {
                        startStreetView(listPanoIds, 3, false);
                    }
                    else if ((sender as Button).Name.Equals("buttonStreetViewQuick"))
                    {
                        startStreetView(listPanoIds, 2, false);
                    }
                    else if ((sender as Button).Name.Equals("buttonOculusView"))
                    {
                        startStreetView(listPanoIds, 3, true);
                    }
                }
            }
        }