public GetStopsForLocationCompleted(string requestUrl, HttpCache stopsCache, StopsForLocation_Callback callback) : base(requestUrl) { this.callback = callback; this.stopsCache = stopsCache; }
public void StopsForLocation(GeoCoordinate location, string query, int radiusInMeters, int maxCount, bool invalidateCache, StopsForLocation_Callback callback) { GeoCoordinate roundedLocation = GetRoundedLocation(location); // ditto for the search radius -- nearest 50 meters for caching int roundedRadius = (int)(Math.Round(radiusInMeters / 50.0) * 50); string requestString = string.Format( "{0}/{1}.xml?key={2}&lat={3}&lon={4}&radius={5}&Version={6}", WebServiceUrlForLocation(location), "stops-for-location", KEY, roundedLocation.Latitude.ToString(NumberFormatInfo.InvariantInfo), roundedLocation.Longitude.ToString(NumberFormatInfo.InvariantInfo), roundedRadius, APIVERSION ); if (string.IsNullOrEmpty(query) == false) { requestString += string.Format("&query={0}", query); } if (maxCount > 0) { requestString += string.Format("&maxCount={0}", maxCount); } Uri requestUri = new Uri(requestString); if (invalidateCache) { stopsCache.Invalidate(requestUri); } stopsCache.DownloadStringAsync(requestUri, new GetStopsForLocationCompleted(requestString, stopsCache, callback).HttpCache_Completed); }
public void StopsForLocation(GeoCoordinate location, string query, int radiusInMeters, int maxCount, bool invalidateCache, StopsForLocation_Callback callback) { GeoCoordinate roundedLocation = GetRoundedLocation(location); // ditto for the search radius -- nearest 50 meters for caching int roundedRadius = (int)(Math.Round(radiusInMeters / 50.0) * 50); string requestUrl = string.Format( "{0}/{1}.xml?key={2}&lat={3}&lon={4}&radius={5}&Version={6}", WebServiceUrlForLocation(location), "stops-for-location", KEY, roundedLocation.Latitude.ToString(NumberFormatInfo.InvariantInfo), roundedLocation.Longitude.ToString(NumberFormatInfo.InvariantInfo), roundedRadius, APIVERSION ); if (string.IsNullOrEmpty(query) == false) { requestUrl += string.Format("&query={0}", query); } if (maxCount > 0) { requestUrl += string.Format("&maxCount={0}", maxCount); } Uri requestUri = new Uri(requestUrl); HttpWebRequest requestGetter = (HttpWebRequest)HttpWebRequest.Create(requestUri); requestGetter.BeginGetResponse( delegate(IAsyncResult asyncResult) { XDocument xmlResponse = null; List <Stop> stops = new List <Stop>(); bool limitExceeded = false; try { xmlResponse = ValidateWebCallback(asyncResult); IDictionary <string, Route> routesMap = ParseAllRoutes(xmlResponse); stops.AddRange(from stop in xmlResponse.Descendants("stop") select ParseStop ( stop, (from routeId in stop.Element("routeIds").Descendants("string") select routesMap[SafeGetValue(routeId)]).ToList <Route>() )); IEnumerable <XElement> descendants = xmlResponse.Descendants("data"); if (descendants.Count() != 0) { limitExceeded = bool.Parse(SafeGetValue(descendants.First().Element("limitExceeded"))); } Debug.Assert(limitExceeded == false); } catch (WebserviceResponseException) { } catch (Exception ex) { Exception error = new WebserviceParsingException(requestUrl, xmlResponse.ToString(), ex); throw error; } callback(stops, limitExceeded); }, requestGetter); }