public void Init(string savedLatestChoiceFileName) { latestChociedStop = new StoppingPlaces(savedLatestChoiceFileName == null ? null : savedLatestChoiceFileName + ".xml"); // Get saved last choiced stop (choiced in dropdown) var items = latestChociedStop.Load(_filesystem); if (items != null && items.Count == 1) { Trigger("savedLatstChoicedStop", items); } }
async void GetNearestStops() { try { StoppingPlaces data = null; if (!_debugLocatoin) { data = await _mapViewModel.GetNearestStops(_currentLocation.Latitude, _currentLocation.Longitude); } else { data = await _mapViewModel.GetNearestStops(57.724215, 11.970159); } if (data.GetList().Count > 0) { /* * Zoom in on first result */ _googleMap.Clear(); var stopList = data.GetList(); var firstStop = stopList[0]; PositionCamera(_currentLocation); _googleMap.InfoWindowClick += (sender, e) => { var stopPlace = stopList.Find(x => x.Name == e.Marker.Title); MapBridge.Instance.FireStoppingPlaceSelcted(stopPlace); }; /* * Create markers */ foreach (StoppingPlace stop in stopList) { _googleMap.AddMarker(new MarkerOptions() .SetPosition(new LatLng(stop.Lat, stop.Lon)) .SetTitle(stop.Name) .SetIcon(BitmapDescriptorFactory.FromResource(stops.Resource.Drawable.marker))); } } } catch (Exception) { Snackbar.Make(_snackBarView, "Gick inte att hämta hållplatser", Snackbar.LengthIndefinite) .SetAction("Fösök igen", (view) => { GetNearestStops(); }).Show(); } }
public async Task <HttpResult> GetNearestStops(double Lat, double Lon) { await GetToken(); var hasAddedStopList = new List <string>(); try { /* * Set up basic autentisering */ using (var client = new HttpClient()) { client.BaseAddress = new Uri(" https://api.vasttrafik.se/bin/rest.exe/v2/"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this._accessToken); var result = await client.GetAsync("location.nearbystops" + "?format=json&maxDist=6500&maxNo=1000&originCoordLat=" + Lat.ToString().Replace(",", ".") + "&originCoordLong=" + Lon.ToString().Replace(",", ".")); result.EnsureSuccessStatusCode(); string resultContent = await result.Content.ReadAsStringAsync(); if (resultContent == null || resultContent.Length < 3) { throw new ArgumentException("Bad data"); } try { var res = JObject.Parse(resultContent); JArray locationListArr = (JArray)res["LocationList"]["StopLocation"]; var stopManager = new StoppingPlaces(); var length = locationListArr.Count; foreach (var obj in (JToken)locationListArr) { var element = hasAddedStopList.Find(e => e == (string)obj.SelectToken("name")); if (element != null) { continue; } stopManager.Add(new StoppingPlace() { Lat = (double)obj.SelectToken("lat"), Lon = (double)obj.SelectToken("lon"), Id = (string)obj.SelectToken("id"), Name = (string)obj.SelectToken("name") }); hasAddedStopList.Add((string)obj.SelectToken("name")); } return(new HttpResult(true, stopManager)); } catch (Exception e) { throw new ArgumentException("Bad data", e.Message); } } } catch (Exception e) { throw new ArgumentException("Bad connection", e.Message); } }
public async Task <HttpResult> GetStops(string text) { await GetToken(); var encoded = System.Net.WebUtility.UrlEncode(text); try { /* * Set up basic autentisering */ using (var client = new HttpClient()) { client.BaseAddress = new Uri(" https://api.vasttrafik.se/bin/rest.exe/v2/"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this._accessToken); var result = await client.GetAsync("location.name" + "?format=json&input=" + encoded); result.EnsureSuccessStatusCode(); string resultContent = await result.Content.ReadAsStringAsync(); if (resultContent == null || resultContent.Length < 3) { throw new ArgumentException("Bad data"); } try { var res = JObject.Parse(resultContent); JArray locationListArr = (JArray)res["LocationList"]["StopLocation"]; var stopManager = new StoppingPlaces(); var length = locationListArr.Count; foreach (var obj in (JToken)locationListArr) { stopManager.Add(new StoppingPlace() { Lat = (double)obj.SelectToken("lat"), Lon = (double)obj.SelectToken("lon"), Id = (string)obj.SelectToken("id"), Name = (string)obj.SelectToken("name") }); } return(new HttpResult(true, stopManager)); //return new HttpResult(true, null); } catch (Exception e) { throw new ArgumentException("Bad data", e.Message); } } } catch (Exception e) { throw new ArgumentException("Bad connection", e.Message); } }