Ejemplo n.º 1
0
        public Station[] GetClosestStationTo(Station[] stations, Func <Station, bool> filter, params GeoPoint[] locations)
        {
            var distanceToGeoPoints = new SortedDictionary <double, Station> [locations.Length];
            var ss = filter == null ? (IEnumerable <Station>)stations : stations.Where(filter);

            foreach (var station in ss)
            {
                for (int i = 0; i < locations.Length; i++)
                {
                    if (distanceToGeoPoints [i] == null)
                    {
                        distanceToGeoPoints [i] = new SortedDictionary <double, Station> ();
                    }
                    distanceToGeoPoints [i].Add(GeoUtils.Distance(locations[i], station.Location), station);
                }
            }

            return(distanceToGeoPoints.Select(ds => ds.First().Value).ToArray());
        }
Ejemplo n.º 2
0
        public void OpenStationWithMarker(Marker marker)
        {
            if (string.IsNullOrEmpty(marker.Title) || marker.Title == SearchPinId)
            {
                return;
            }

            var    splitTitle = marker.Title.Split('|');
            string displayNameSecond;
            var    displayName = StationUtils.CutStationName(splitTitle [1], out displayNameSecond);

            ipName.Text  = displayName;
            ipName2.Text = displayNameSecond;

            currentShownID     = int.Parse(splitTitle [0]);
            currentShownMarker = marker;

            var isLocked = string.IsNullOrEmpty(marker.Snippet);

            if (ipStationLock == null)
            {
                ipStationLock = pane.FindViewById(Resource.Id.stationLock);
            }
            ipStationLock.Visibility = isLocked ? ViewStates.Visible : ViewStates.Gone;

            if (!isLocked)
            {
                var splitNumbers = marker.Snippet.Split('|');
                ipBikes.Text = splitNumbers [0];
                ipSlots.Text = splitNumbers [1];

                var baseGreen = Color.Rgb(0x66, 0x99, 0x00);
                var baseRed   = Color.Rgb(0xcc, 0x00, 0x00);
                var bikesNum  = int.Parse(splitNumbers [0]);
                var slotsNum  = int.Parse(splitNumbers [1]);
                var total     = bikesNum + slotsNum;
                var distance  = GeoUtils.Distance(
                    new GeoPoint {
                    Lat = marker.Position.Latitude, Lon = marker.Position.Longitude
                },
                    new GeoPoint {
                    Lat = map.MyLocation.Latitude, Lon = map.MyLocation.Longitude
                }
                    ) * 1000;
                var bikesColor = PinFactory.InterpolateColor(baseRed, baseGreen,
                                                             ((float)bikesNum) / total);
                var slotsColor = PinFactory.InterpolateColor(baseRed, baseGreen,
                                                             ((float)slotsNum) / total);
                ipBikes.SetTextColor(bikesColor);
                ipSlots.SetTextColor(slotsColor);

                DrawableCompat.SetTint(bikeDrawable,
                                       bikesColor.ToArgb());
                DrawableCompat.SetTint(rackDrawable,
                                       slotsColor.ToArgb());
                ipDistance.Text = GeoUtils.GetDisplayDistance(distance)
                                  + " " + GeoUtils.GetUnitForDistance(distance);

                ipDistance.Visibility = ViewStates.Visible;
                ipBikes.Visibility    = ViewStates.Visible;
                ipSlots.Visibility    = ViewStates.Visible;
                ipBikesImg.Visibility = ViewStates.Visible;
                ipSlotsImg.Visibility = ViewStates.Visible;
            }
            else
            {
                ipDistance.Visibility = ViewStates.Gone;
                ipBikes.Visibility    = ViewStates.Invisible;
                ipSlots.Visibility    = ViewStates.Invisible;
                ipBikesImg.Visibility = ViewStates.Invisible;
                ipSlotsImg.Visibility = ViewStates.Invisible;
            }

            var  favs      = favManager.LastFavorites ?? favManager.GetFavoriteStationIds();
            bool activated = favs.Contains(currentShownID);

            fab.Checked = activated;
            fab.JumpDrawablesToCurrentState();

            if (streetPanorama != null)
            {
                streetPanorama.SetPosition(marker.Position);
            }

            LoadStationHistory(currentShownID);

            pane.SetState(InfoPane.State.Opened);
        }
Ejemplo n.º 3
0
        async void HandleMessage(IMessageEvent message)
        {
            try {
                Android.Util.Log.Info("WearIntegration", "Received Message");
                var client = new GoogleApiClientBuilder(this)
                             .AddApi(LocationServices.API)
                             .AddApi(WearableClass.API)
                             .Build();

                var result = client.BlockingConnect(30, Java.Util.Concurrent.TimeUnit.Seconds);
                if (!result.IsSuccess)
                {
                    return;
                }

                var path = message.Path;

                try {
                    var stations = Hubway.Instance.LastStations;
                    if (stations == null)
                    {
                        stations = await Hubway.Instance.GetStations();
                    }

                    if (path.StartsWith(SearchStationPath))
                    {
                        var lastLocation = LocationServices.FusedLocationApi.GetLastLocation(client);
                        if (lastLocation == null)
                        {
                            return;
                        }

                        var currentPoint = new GeoPoint {
                            Lat = lastLocation.Latitude,
                            Lon = lastLocation.Longitude
                        };
                        var nearestStations = Hubway.GetStationsAround(stations, currentPoint, maxItems: 6, minDistance: double.MaxValue);
                        var favManager      = FavoriteManager.Obtain(this);
                        var favorites       = await favManager.GetFavoriteStationIdsAsync();

                        var request = PutDataMapRequest.Create(SearchStationPath + "/Answer");
                        var map     = request.DataMap;

                        var stationMap = new List <DataMap> ();
                        foreach (var station in nearestStations)
                        {
                            var itemMap = new DataMap();

                            itemMap.PutInt("Id", station.Id);

                            var asset = await CreateWearAssetFrom(station);

                            itemMap.PutAsset("Background", asset);

                            string secondary;
                            string primary = StationUtils.CutStationName(station.Name, out secondary);
                            itemMap.PutString("Primary", primary);
                            itemMap.PutString("Secondary", secondary);

                            var distance = GeoUtils.Distance(currentPoint, station.Location);
                            itemMap.PutDouble("Distance", distance);
                            itemMap.PutDouble("Lat", station.Location.Lat);
                            itemMap.PutDouble("Lon", station.Location.Lon);

                            itemMap.PutInt("Bikes", station.BikeCount);
                            itemMap.PutInt("Racks", station.EmptySlotCount);

                            itemMap.PutBoolean("IsFavorite", favorites.Contains(station.Id));

                            stationMap.Add(itemMap);
                        }
                        map.PutDataMapArrayList("Stations", stationMap);
                        map.PutLong("UpdatedAt", DateTime.UtcNow.Ticks);

                        await WearableClass.DataApi.PutDataItem(client, request.AsPutDataRequest());
                    }
                    else
                    {
                        var uri   = new Uri("wear://watch" + path);
                        var query = uri.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                        var parts = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped).Split('/');

                        var action = parts[parts.Length - 2];
                        var id     = int.Parse(parts.Last());

                        if (action == FavoriteAction)
                        {
                            var favorites = FavoriteManager.Obtain(this);
                            handler.Post(() => {
                                if (query == "add")
                                {
                                    favorites.AddToFavorite(id);
                                }
                                else
                                {
                                    favorites.RemoveFromFavorite(id);
                                }
                            });
                        }
                    }
                } finally {
                    client.Disconnect();
                }
            } catch (Exception e) {
                Android.Util.Log.Error("WearIntegration", e.ToString());
                AnalyticsHelper.LogException("WearIntegration", e);
            }
        }