コード例 #1
0
        private async void UpdateBusPins()
        {
            while (true)
            {
                // skip update pins if map is freezed (user clicks on pin)
                if (!FreezeMap)
                {
                    // remove all bus pins
                    foreach (CustomPin p in map.BusPins)
                    {
                        map.Pins.Remove(p.Pin);
                    }
                    map.BusPins.Clear();

                    foreach (BusOnRoad bor in BusHelper.ActiveBuses.Values)
                    {
                        // move bus to next checkpoint on the route for simulation
                        // actual deployment: get real-time position of bus
                        BusSimulator.GoToNextCheckpoint(bor);

                        // add pin to map if service is to be shown on map
                        if (BusHelper.BusSvcs [bor.routeName].showOnMap)
                        {
                            var description = "Start: " + BusHelper.BusStops [bor.firstStop].name + "\n" +
                                              "End: " + BusHelper.BusStops [bor.lastStop].name + "\n" +
                                              "Approaching: " + BusHelper.BusStops [(string)bor.nextStopEnumerator.Current].name + "\n" +
                                              "In: " + BusHelper.GetArrivalTiming(bor.vehiclePlate) + "\n";
                            var pin = new Pin {
                                Type     = PinType.Place,
                                Position = new Xamarin.Forms.Maps.Position(bor.latitude, bor.longitude),
                                Label    = bor.routeName,
                                Address  = description
                            };
                            var bus = new CustomPin {
                                Pin = pin,
                                Id  = bor.routeName,
                                Url = bor.routeName + ".png"
                            };
                            map.Pins.Add(pin);
                            map.BusPins.Add(bus);
                        }
                    }

                    // remove buses which has finished plying
                    List <BusOnRoad> finishedBuses = BusHelper.ActiveBuses.Values.Where(bor => bor.finished).ToList();
                    foreach (BusOnRoad bor in finishedBuses)
                    {
                        BusHelper.RemoveBusOnRoad(bor.vehiclePlate);
                    }
                }

                // continue after interval
                await Task.Delay(TimeSpan.FromSeconds(SettingsVars.Variables ["REFRESH_BUS_INTERVAL"].value));
            }
        }
コード例 #2
0
ファイル: MapPage.cs プロジェクト: joycelynng/NUSBusMap
        private bool UpdatePositions()
        {
            // skip update pins if map is freezed
            if (FreezeMap)
            {
                return(true);
            }

            // remove all bus pins
            foreach (CustomPin p in map.BusPins)
            {
                map.Pins.Remove(p.Pin);
            }
            map.BusPins.Clear();

            foreach (BusOnRoad bor in BusHelper.ActiveBuses.Values)
            {
                // temp change position randomly for test
                // bor.latitude += rand.NextDouble () * 0.0001 - 0.00005;
                // bor.longitude += rand.NextDouble () * 0.0001 - 0.00005;

                // move bus to next checkpoint on the route for simulation
                BusSimulator.GoToNextCheckpoint(bor);

                // add pin to map if svc show on map
                if (BusHelper.BusSvcs [bor.routeName].showOnMap)
                {
                    var description = "Start: " + BusHelper.BusStops [bor.firstStop].name + "\n" +
                                      "End: " + BusHelper.BusStops [bor.lastStop].name + "\n" +
                                      "Approaching: " + BusHelper.BusStops [(string)bor.nextStopEnumerator.Current].name + "\n" +
                                      "In: " + BusHelper.GetArrivalTiming((string)bor.nextStopEnumerator.Current, bor.routeName);
                    var pin = new Pin {
                        Type     = PinType.Place,
                        Position = new Xamarin.Forms.Maps.Position(bor.latitude, bor.longitude),
                        Label    = bor.routeName,
                        Address  = description
                    };
                    var bus = new CustomPin {
                        Pin = pin,
                        Id  = bor.routeName,
                        Url = bor.routeName + ".png"
                    };
                    map.Pins.Add(pin);
                    map.BusPins.Add(bus);
                }
            }

            return(true);
        }