Beispiel #1
0
        private void MapFocusCreated(object sender, RoutedEventArgs e)
        {
            Map TheMap = new Map();

            if (App.MemoTemp.Latitude != null && App.MemoTemp.Longitude != null)
            {
                TheMap.Center = new System.Device.Location.GeoCoordinate(App.MemoTemp.Latitude.Value, App.MemoTemp.Longitude.Value);
            }

            Microsoft.Phone.Maps.Toolkit.Pushpin pin = new Microsoft.Phone.Maps.Toolkit.Pushpin();
            pin.GeoCoordinate = TheMap.Center;
            var layer   = new MapLayer();
            var overlay = new MapOverlay();

            overlay.GeoCoordinate = TheMap.Center;
            overlay.Content       = pin;
            layer.Add(overlay);
            TheMap.Layers.Add(layer);
            TheMap.ZoomLevel = 16;
            TheMap.Height    = 318;
            Grid.SetRow(TheMap, 4);
            ContentPanel.Children.Add(TheMap);
        }
//------------------------------ Finding the bus stop if the Source is provided ---------------------------//
        
        
        public async void findbusstop(string str)
        {
            String[] busstops = new String[20];
            busstops[0] = "Tollygunge";
            busstops[1] = "Bangur Hospital";
            busstops[2] = "Rashbehari More";
            busstops[3] = "Sarat Bose Road Xing";
            busstops[4] = "Basanti Devi College";
            busstops[5] = "Gariahat";
            busstops[6] = "Bakultala P.O";
            busstops[7] = "Narkelbagan";
            busstops[8] = "Ruby";
            busstops[9] = "Vip Bazar";
            busstops[10] = "Panchannagram";
            busstops[11] = "Science City";
            busstops[12] = "Chingrighata";
            busstops[13] = "Beliaghata";
            busstops[14] = "Hyatt Regency";
            busstops[15] = "Ultadanga";
            busstops[16] = "Lake Town";
            busstops[17] = "Dum Dum Park";
            busstops[18] = "Baguihati";
            busstops[19] = "Kaikhali";

            for(int a = 0; a < 20; a++)
            {
                if(busstops[a].Equals(str))
                {
                    find = getgeoCoords(a);
                }
            }

            myposition();
            GeoCoordinate g1 = new GeoCoordinate(l1,l2);

            var query = new RouteQuery();
            query.Waypoints = new[]
            {
                g1,
                find,
            };

            query.TravelMode = TravelMode.Driving;
            var result = await query.GetRouteAsync();
            Route myroute = result;
            double dis = myroute.LengthInMeters;
            dis = dis / 1000;
            if (me2bus != null) kmap.RemoveRoute(me2bus);
            me2bus = new MapRoute(myroute);
            me2bus.Color = Colors.Orange;
            kmap.AddRoute(me2bus);
            query.Dispose();

            text1.Text = "Nearest Bus Stop : " + str;
            text1.IsReadOnly = true;
            text1.Opacity = 1;

            text2.Text = dis + " Km";
            text2.Opacity = 1;
            text2.IsReadOnly = true;

            Microsoft.Phone.Maps.Toolkit.Pushpin push3 = new Microsoft.Phone.Maps.Toolkit.Pushpin();
            push3.Content = "     Bus-Stop   ";
            push3.FontSize = 18;
            push3.GeoCoordinate = find;
            push3.Opacity = 0.7;
            push3.Background = new SolidColorBrush(Colors.Green);
            push3.Foreground = new SolidColorBrush(Colors.White);

            kmap.Layers.Remove(stopPos);
            kmap.Layers.Remove(stopPosDot);

            MapOverlay myoverlay3 = new MapOverlay();
            myoverlay3.Content = push3;
            myoverlay3.GeoCoordinate = find;
            myoverlay3.PositionOrigin = new Point(0, 1);
            stopPos = new MapLayer();
            stopPos.Add(myoverlay3);
            kmap.Layers.Add(stopPos);
            stopPosDot = ShowLocation(find);
        }
//---------------------------------------- Gets the location of the User ----------------------------------//



        public void mylocation()
        {
            kmap.Layers.Remove(myPos);
            kmap.Layers.Remove(myPosDot);
            GeoCoordinate g1 = new GeoCoordinate(l1,l2);
            Microsoft.Phone.Maps.Toolkit.Pushpin push2 = new Microsoft.Phone.Maps.Toolkit.Pushpin();
            push2.Content = "      You       ";
            push2.FontSize = 18;
            push2.GeoCoordinate = g1;
            push2.Opacity = 0.7;
            push2.Background = new SolidColorBrush(Colors.Green);
            push2.Foreground = new SolidColorBrush(Colors.White);

            MapOverlay myoverlay2 = new MapOverlay();
            myoverlay2.Content = push2;
            myoverlay2.GeoCoordinate = g1;
            myoverlay2.PositionOrigin = new Point(0, 1);
            myPos = new MapLayer();
            myPos.Add(myoverlay2);
            kmap.Layers.Add(myPos);
            myPosDot = ShowLocation(g1);

            if(flag==0)
            {
                String url = "http://findmybus.herokuapp.com/routes?route=v1&lat=" + l1 + "&lon=" + l2;
                WebClient wc = new WebClient();
                wc.DownloadStringAsync(new Uri(url), UriKind.Relative);
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(opencompletedstring);
            }
        }
        public async void opencompletedstring(object sender, DownloadStringCompletedEventArgs e)
        {
            var myData = e.Result;
            var json = JsonConvert.DeserializeObject<MapData>(myData);
            double lat2 = json.lat;
            double lon2 = json.lon;
            int i = json.index;
            myStopPos = i;
            double distance = json.distance;
            double dis = distance / 1000;

            GeoCoordinate g1 = new GeoCoordinate(l1, l2);
            GeoCoordinate g2 = new GeoCoordinate(lat2, lon2);

            String[] busstops = new String[20];
            busstops[0] = "Tollygunge";
            busstops[1] = "Bangur Hospital";
            busstops[2] = "Rashbehari More";
            busstops[3] = "Sarat Bose Road Xing";
            busstops[4] = "Basanti Devi College";
            busstops[5] = "Gariahat";
            busstops[6] = "Bakultala P.O";
            busstops[7] = "Narkelbagan";
            busstops[8] = "Ruby";
            busstops[9] = "Vip Bazar";
            busstops[10] = "Panchannagram";
            busstops[11] = "Science City";
            busstops[12] = "Chingrighata";
            busstops[13] = "Beliaghata";
            busstops[14] = "Hyatt Regency";
            busstops[15] = "Ultadanga";
            busstops[16] = "Lake Town";
            busstops[17] = "Dum Dum Park";
            busstops[18] = "Baguihati";
            busstops[19] = "Kaikhali";

            text1.Text = "Nearest Bus Stop : " + busstops[i - 1];
            text1.IsReadOnly = true;
            text1.Opacity = 1;

            text2.Text = dis + " Km";
            text2.Opacity = 1;
            text2.IsReadOnly = true;

            var query = new RouteQuery();
            query.Waypoints = new[]
            {
                g1,
                g2,
            };

            if(me2bus!=null) kmap.RemoveRoute(me2bus);
            query.TravelMode = TravelMode.Driving;
            var result = await query.GetRouteAsync();
            Route myroute = result;
            me2bus = new MapRoute(myroute);
            me2bus.Color = Colors.Orange;
            kmap.AddRoute(me2bus);
            query.Dispose();

            kmap.Layers.Remove(stopPos);
            kmap.Layers.Remove(stopPosDot);

            Microsoft.Phone.Maps.Toolkit.Pushpin push3 = new Microsoft.Phone.Maps.Toolkit.Pushpin();
            push3.Content = "     Bus-Stop   ";
            push3.FontSize = 18;
            push3.GeoCoordinate = g2;
            push3.Opacity = 0.7;
            push3.Background = new SolidColorBrush(Colors.Green);
            push3.Foreground = new SolidColorBrush(Colors.White);

            MapOverlay myoverlay3 = new MapOverlay();
            myoverlay3.Content = push3;
            myoverlay3.GeoCoordinate = g2;
            myoverlay3.PositionOrigin = new Point(0,1);
            stopPos = new MapLayer();
            stopPos.Add(myoverlay3);
            kmap.Layers.Add(stopPos);
            stopPosDot = ShowLocation(g2);
        }
        public void nowcompletedstring(object sender, DownloadStringCompletedEventArgs e)
        {
            double dis = 0.0;
            GeoCoordinate geotest = new GeoCoordinate();
            kmap.Layers.Remove(mylayer);
            kmap.Layers.Remove(loclay2);
            var myData = e.Result;
            var json = JsonConvert.DeserializeObject<Mappresent>(myData);
            double lat2 = json.lat;
            double lon2 = json.lon;
            GeoCoordinate g1 = new GeoCoordinate(lat2, lon2);

            Microsoft.Phone.Maps.Toolkit.Pushpin push = new Microsoft.Phone.Maps.Toolkit.Pushpin();
            push.Content = "Your bus is here";
            push.FontSize = 18;
            push.GeoCoordinate = g1;
            push.Opacity = 0.7;
            push.Background = new SolidColorBrush(Colors.Green);
            push.Foreground = new SolidColorBrush(Colors.White);
            MapOverlay myoverlay = new MapOverlay();
            myoverlay.Content = push;
            myoverlay.GeoCoordinate = g1;
            myoverlay.PositionOrigin = new Point(0,1);
            mylayer = new MapLayer();
            mylayer.Add(myoverlay);
            kmap.Layers.Add(mylayer);

            var myImage = new BitmapImage(new Uri("/Assets/bus.png", UriKind.Relative));
            var image = new Image();
            image.Width = 30;
            image.Height = 30;
            image.Opacity = 50;

            image.Source = myImage;
            MapOverlay myLocationOverlay2 = new MapOverlay();
            myLocationOverlay2.Content = image;
            myLocationOverlay2.PositionOrigin = new Point(0.5, 0.5);
            myLocationOverlay2.GeoCoordinate = g1;
            loclay2 = new MapLayer();
            loclay2.Add(myLocationOverlay2);
            kmap.Layers.Add(loclay2);
                      


//------------------------ Calculating distance between the bus and the Bus stop !!-----------------------//


            String url = "http://findmybus.herokuapp.com/routes?route=v1&lat=" + lat2 + "&lon=" + lon2;
            WebClient wc = new WebClient();
            wc.DownloadStringAsync(new Uri(url), UriKind.Relative);
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnServerResult);
            
        }