Beispiel #1
0
		private async Task<GetGeoCoordinatesResponse> GetCoordinatesAsync(GetGeoCoordinatesRequest request)
		{
			var response = new GetGeoCoordinatesResponse();

			using (var locationRequest = new HttpClient())
			{
				locationRequest.Timeout = new TimeSpan(0, 30, 0);
				var url = GeolocationUrl + "?address=" + Uri.EscapeUriString(request.PostCode);
				using (var responseJson = await locationRequest.GetAsync(url))
				{
					if (responseJson.IsSuccessStatusCode)
					{
						var content = await responseJson.Content.ReadAsStringAsync();
						dynamic location = JsonConvert.DeserializeObject(content);
						if (location.status == "OK")
						{
							var addressDetails = location.results[0];
							double longitude, latitude;
							if (double.TryParse(addressDetails.geometry.location.lat.ToString(), out latitude) & double.TryParse(addressDetails.geometry.location.lng.ToString(), out longitude))
							{
								response.Status = StatusCode.OK;
								response.GeoCoordinate = new GeoCoordinate(latitude, longitude);
							}
						}
					}
					else
					{
						this.exceptionHandler.HandleException(new Exception("GetGeoCoordinatesAsync: " + responseJson.ReasonPhrase));
						response.Status = StatusCode.InternalServerError;
					}
				}
			}

			return response;
		}
Beispiel #2
0
		public async Task<double?> GetDistanceAsync(GetGeoCoordinatesRequest startPoint, GetGeoCoordinatesRequest endPoint, DistanceUnit distanceUnit)
		{
			if (startPoint != null && endPoint != null)
			{
				var url = DistanceUrl + string.Format("?origins={0}&destinations={1}&language=en-gb", Uri.EscapeUriString(startPoint.PostCode), Uri.EscapeUriString(endPoint.PostCode));

				return await GetDistanceAsync(url, distanceUnit);
			}

			return null;
		}
Beispiel #3
0
        public async Task <double?> GetDistanceAsync(GetGeoCoordinatesRequest startPoint, GetGeoCoordinatesRequest endPoint, DistanceUnit distanceUnit)
        {
            if (startPoint != null && endPoint != null)
            {
                var url = DistanceUrl + string.Format("?origins={0}&destinations={1}&language=en-gb", Uri.EscapeUriString(startPoint.PostCode), Uri.EscapeUriString(endPoint.PostCode));

                return(await GetDistanceAsync(url, distanceUnit));
            }

            return(null);
        }
Beispiel #4
0
		public async Task<GetGeoCoordinatesResponse> GetGeoCoordinatesAsync(GetGeoCoordinatesRequest request)
		{
			var response = new GetGeoCoordinatesResponse();

			if (!string.IsNullOrWhiteSpace(request.PostCode))
			{
				try
				{
					response = await this.GetCoordinatesAsync(request);
				}
				catch (Exception ex)
				{
					this.exceptionHandler.HandleException(ex);
					response.Status = StatusCode.InternalServerError;
				}
			}
			else
			{
				response.Status = StatusCode.BadRequest;
			}

			return response;
		}
Beispiel #5
0
        public async Task <GetGeoCoordinatesResponse> GetGeoCoordinatesAsync(GetGeoCoordinatesRequest request)
        {
            var response = new GetGeoCoordinatesResponse();

            if (!string.IsNullOrWhiteSpace(request.PostCode))
            {
                try
                {
                    response = await this.GetCoordinatesAsync(request);
                }
                catch (Exception ex)
                {
                    this.exceptionHandler.HandleException(ex);
                    response.Status = StatusCode.InternalServerError;
                }
            }
            else
            {
                response.Status = StatusCode.BadRequest;
            }

            return(response);
        }
Beispiel #6
0
        private async Task <GetGeoCoordinatesResponse> GetCoordinatesAsync(GetGeoCoordinatesRequest request)
        {
            var response = new GetGeoCoordinatesResponse();

            using (var locationRequest = new HttpClient())
            {
                locationRequest.Timeout = new TimeSpan(0, 30, 0);
                var url = GeolocationUrl + "?address=" + Uri.EscapeUriString(request.PostCode);
                using (var responseJson = await locationRequest.GetAsync(url))
                {
                    if (responseJson.IsSuccessStatusCode)
                    {
                        var content = await responseJson.Content.ReadAsStringAsync();

                        dynamic location = JsonConvert.DeserializeObject(content);
                        if (location.status == "OK")
                        {
                            var    addressDetails = location.results[0];
                            double longitude, latitude;
                            if (double.TryParse(addressDetails.geometry.location.lat.ToString(), out latitude) & double.TryParse(addressDetails.geometry.location.lng.ToString(), out longitude))
                            {
                                response.Status        = StatusCode.OK;
                                response.GeoCoordinate = new GeoCoordinate(latitude, longitude);
                            }
                        }
                    }
                    else
                    {
                        this.exceptionHandler.HandleException(new Exception("GetGeoCoordinatesAsync: " + responseJson.ReasonPhrase));
                        response.Status = StatusCode.InternalServerError;
                    }
                }
            }

            return(response);
        }