public static void DumpLocation(GeoPoint p, Stream stream) { using (var writer = new BinaryWriter (stream, System.Text.Encoding.UTF8, true)) { writer.Write (p.Lat); writer.Write (p.Lon); } }
// Use spherical law of cosines public static double Distance(GeoPoint p1, GeoPoint p2) { const int EarthRadius = 6371; return Math.Acos (Math.Sin (DegToRad (p1.Lat)) * Math.Sin (DegToRad (p2.Lat)) + Math.Cos (DegToRad (p1.Lat)) * Math.Cos (DegToRad (p2.Lat)) * Math.Cos (DegToRad (p2.Lon) - DegToRad (p1.Lon))) * EarthRadius; }
public static StationCardFragment WithStation (Station station, GeoPoint currentLocation, BikeActionStatus status) { var r = new StationCardFragment (); r.station = station; r.currentLocation = currentLocation; r.status = status; return r; }
internal async void LoadMap (GeoPoint point, FavoriteView view, ImageView mapView, long version) { var url = MakeMapUrl (point); var bmp = await LoadInternal (url, MapCache); if (bmp != null && view.VersionNumber == version) { mapView.AlphaAnimate (0, duration: 150, endAction: () => { mapView.SetImageDrawable (new RoundCornerDrawable (bmp)); mapView.AlphaAnimate (1, duration: 250); }); } }
public static string MakeStreetViewUrl(GeoPoint position, int width, int height) { var parameters = new Dictionary<string, string> { { "key", ApiKey }, { "sensor", "false" }, { "size", Math.Min (width, MaxStreetViewSize) + "x" + Math.Min (height, MaxStreetViewSize) }, { "location", position.Lat.ToSafeString () + "," + position.Lon.ToSafeString () }, { "pitch", "-10" }, }; return BuildUrl ("https://maps.googleapis.com/maps/api/streetview?", parameters); }
public static string MakeMapUrl(GeoPoint location, int width, int height) { var zoom = "17"; var markerSize = "size:med"; var parameters = new Dictionary<string, string> { { "key", ApiKey }, { "zoom", zoom }, { "sensor", "true" }, { "size", width + "x" + height }, { "center", location.Lat + "," + location.Lon }, { "markers", markerSize + "|" + location.Lat + "," + location.Lon } }; return BuildUrl ("https://maps.googleapis.com/maps/api/staticmap?", parameters); }
public static string MakeMapUrl (GeoPoint location) { const int Width = 65; const int Height = 42; var parameters = new Dictionary<string, string> { { "key", ApiKey }, { "zoom", "13" }, { "sensor", "true" }, { "size", Width.ToPixels () + "x" + Height.ToPixels () }, { "center", location.Lat + "," + location.Lon }, { "markers", "size:tiny|" + location.Lat + "," + location.Lon } }; return BuildUrl ("https://maps.googleapis.com/maps/api/staticmap?", parameters); }
async void HandleMessage (IMessageEvent message) { try { Android.Util.Log.Info ("WearIntegration", "Received Message"); var client = new GoogleApiClientBuilder (this) .AddApi (WearableClass.Api) .AddApi (LocationServices.Api) .Build (); var result = client.BlockingConnect (30, Java.Util.Concurrent.TimeUnit.Seconds); if (!result.IsSuccess) return; try { var lastLocation = LocationServices.FusedLocationApi.GetLastLocation (client); if (lastLocation == null) return; var stations = Hubway.Instance.LastStations; if (stations == null) stations = await Hubway.Instance.GetStations (); var currentPoint = new GeoPoint { Lat = lastLocation.Latitude, Lon = lastLocation.Longitude }; var nearestStations = Hubway.GetStationsAround (stations, currentPoint); using (var stream = new System.IO.MemoryStream ()) { GeoUtils.DumpLocation (currentPoint, stream); StationUtils.DumpStations (nearestStations, stream); var bytes = stream.ToArray (); WearableClass.MessageApi.SendMessage (client, message.SourceNodeId, SearchStationPath + "/Answer", bytes).Await (); } } finally { client.Disconnect (); } } catch (Exception e) { Android.Util.Log.Error ("WearIntegration", e.ToString ()); } }
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); } }
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); 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); } }