Exemplo n.º 1
0
        public override double GetElevationFromWebService(double lat, double lng)
        {
            if (string.IsNullOrEmpty(ApiKey))
            {
                _logger.Trace("No Google API key set, returning elevation 0.");
                return(0);
            }

            double elevationInMeters = 0;

            try
            {
                string         url     = $"https://maps.googleapis.com/maps/api/elevation/json?key={ApiKey}&locations={lat},{lng}";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Credentials      = CredentialCache.DefaultCredentials;
                request.UserAgent        = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
                request.ContentType      = "application/json";
                request.ReadWriteTimeout = 2000;

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string         responseFromServer = reader.ReadToEnd();
                            GoogleResponse googleResponse     = JsonConvert.DeserializeObject <GoogleResponse>(responseFromServer);

                            if (googleResponse.Status != "OK")
                            {
                                _logger.Warn("Request to Google API failed: {0}", googleResponse.ErrorMessage);
                            }
                            else
                            {
                                if (googleResponse.Results != null && googleResponse.Results.Count != 0)
                                {
                                    elevationInMeters = googleResponse.Results[0].Elevation;
                                }
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Could not get elevation/altitude. Returning 0.");
                elevationInMeters = 0;
            }

            return(elevationInMeters);
        }
Exemplo n.º 2
0
        public override double GetElevationFromWebService(double lat, double lng)
        {
            if (string.IsNullOrEmpty(_apiKey))
            {
                return(0);
            }

            try
            {
                string         url     = $"https://maps.googleapis.com/maps/api/elevation/json?key={_apiKey}&locations={lat},{lng}";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Credentials      = CredentialCache.DefaultCredentials;
                request.UserAgent        = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
                request.ContentType      = "application/json";
                request.ReadWriteTimeout = 2000;
                string responseFromServer = "";

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            responseFromServer = reader.ReadToEnd();
                            GoogleResponse googleResponse = JsonConvert.DeserializeObject <GoogleResponse>(responseFromServer);

                            if (googleResponse.status == "OK" && googleResponse.results != null &&
                                0 < googleResponse.results.Count && googleResponse.results[0].elevation > -100)
                            {
                                return(googleResponse.results[0].elevation);
                            }

                            // All error handling is handled inside of the ElevationService.
                        }
                }
            }
            catch (ActiveSwitchByRuleException ex)
            {
                throw ex;
            }

            catch (Exception)
            {
                // If we get here for any reason, then just drop down and return 0.
            }

            return(0);
        }
Exemplo n.º 3
0
        public override async Task <double> GetElevationFromWebService(double lat, double lng)
        {
            if (string.IsNullOrEmpty(_apiKey))
            {
                return(0);
            }

            using (HttpClient client = new HttpClient())
            {
                try
                {
                    string url = $"https://maps.googleapis.com/maps/api/elevation/json?key={_apiKey}&locations={lat},{lng}";

                    var responseContent = await client.GetAsync(url).ConfigureAwait(false);

                    if (responseContent.StatusCode != HttpStatusCode.OK)
                    {
                        return(0);
                    }

                    var responseFromServer = await responseContent.Content.ReadAsStringAsync().ConfigureAwait(false);

                    GoogleResponse googleResponse = JsonConvert.DeserializeObject <GoogleResponse>(responseFromServer);

                    if (googleResponse.Status == "OK" && googleResponse.Results != null &&
                        0 < googleResponse.Results.Count && googleResponse.Results[0].Elevation > -100)
                    {
                        return(googleResponse.Results[0].Elevation);
                    }
                }
                catch (ActiveSwitchByRuleException ex)
                {
                    throw ex;
                }
                catch (Exception)
                {
                    // If we get here for any reason, then just drop down and return 0. Will cause this elevation service to be blacklisted.
                }
            }

            return(0);
        }