Example #1
0
        /// <summary>
        /// Calculates a route
        /// </summary>
        /// <param name="origin">The origin</param>
        /// <param name="destination">The destination</param>
        /// <param name="mode">The travelling mode</param>
        /// <param name="language">The language</param>
        /// <returns>A <see cref="GmsDirectionResult"/></returns>
        public async Task<GmsDirectionResult> CalculateRoute(Position origin, Position destination, GmsDirectionTravelMode mode, string language = null)
        {
            var response = await _httpClient.GetAsync(this.BuildQueryString(origin, destination, mode, language));

            if (response.IsSuccessStatusCode)
            {
                return JsonConvert.DeserializeObject<GmsDirectionResult>(await response.Content.ReadAsStringAsync());
            }
            return null;
        }
Example #2
0
        /// <summary>
        /// Builds the query string for the Google Maps Directions API call
        /// </summary>
        /// <param name="origin">The origin</param>
        /// <param name="destination">The destination</param>
        /// <param name="mode">The travelling mode</param>
        /// <param name="language">The language</param>
        /// <returns>The query string</returns>
        private string BuildQueryString(Position origin, Position destination, GmsDirectionTravelMode mode, string language)
        {
            StringBuilder strBuilder = new StringBuilder(
                string.Format(
                    "json?origin={0}&destination={1}&mode={2}", 
                    origin.AsString(),
                    destination.AsString(),
                    mode.ToString().ToLower()));

            if (!string.IsNullOrWhiteSpace(language))
            {
                strBuilder.AppendFormat("&language={0}", language);
            }
            strBuilder.AppendFormat("&key={0}", _apiKey);
            return strBuilder.ToString();
        }