// adds route
      private void button12_Click(object sender, RoutedEventArgs e)
      {
         RoutingProvider rp = MainMap.MapProvider as RoutingProvider;
         if(rp == null)
         {
            rp = GMapProviders.OpenStreetMap; // 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);
         }
      }
        /// <summary>
        /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
        /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for this serialization.</param>
        /// <exception cref="T:System.Security.SecurityException">
        /// The caller does not have the required permission.
        /// </exception>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Id", this.Id);
            info.AddValue("IsVisible", this.IsVisibile);

            GMapMarker[] markerArray = new GMapMarker[this.Markers.Count];
            this.Markers.CopyTo(markerArray, 0);
            info.AddValue("Markers", markerArray);

            GMapRoute[] routeArray = new GMapRoute[this.Routes.Count];
            this.Routes.CopyTo(routeArray, 0);
            info.AddValue("Routes", routeArray);

            GMapPolygon[] polygonArray = new GMapPolygon[this.Polygons.Count];
            this.Polygons.CopyTo(polygonArray, 0);
            info.AddValue("Polygons", polygonArray);
        }
        // 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);
                    }
                }
            }
        }