Esempio n. 1
0
        public GoogleElevationResponse RequestElevation(IGeoLatLon point)
        {
            var location = GetLocationString(point);

            var key = location;

            if (_cacheElevation.ContainsKey(key))
            {
                return(_cacheElevation[key]);
            }

            var client  = new RestClient("https://maps.googleapis.com");
            var request = new RestRequest("maps/api/elevation/json", Method.GET);

            request.AddParameter("key", _apiKey);
            request.AddParameter("location", location);
            request.AddParameter("sensor", "false");

            var response = client.Execute <GoogleElevationResponse>(request);

            lock (_cacheElevation) { if (!_cacheElevation.ContainsKey(key))
                                     {
                                         _cacheElevation.Add(key, response.Data);
                                     }
            }
            return(response.Data);
        }
Esempio n. 2
0
        public GoogleLocationResponse RequestLocations(IGeoLatLon point)
        {
            var location = GetLocationString(point);

            var key = location;

            if (_cacheLocation.ContainsKey(key))
            {
                return(_cacheLocation[key]);
            }

            var client  = new RestClient("https://maps.googleapis.com");
            var request = new RestRequest("maps/api/geocode/json", Method.GET);

            request.AddParameter("key", _apiKey);
            request.AddParameter("latlng", location);

            var response = client.Execute(request);
            var data     = new GoogleLocationResponse(response.Content);

            //dynamic data = JsonConvert.DeserializeObject(response.Content);

            lock (_cacheLocation) { if (!_cacheLocation.ContainsKey(key))
                                    {
                                        _cacheLocation.Add(key, data);
                                    }
            }
            return(data);
        }
Esempio n. 3
0
        // ==================================================
        // Methods
        public GoogleTimezoneResponse RequestTimezone(IGeoLatLon point, DateTime?timestamp = null)
        {
            var location = GetLocationString(point);

            if (!timestamp.HasValue)
            {
                timestamp = DateTime.Today;
            }
            var unixTimestamp = GetUnixTimeStampFromDateTime(timestamp.Value);

            var key = $"{location}_{unixTimestamp}";

            if (_cacheTimezone.ContainsKey(key))
            {
                return(_cacheTimezone[key]);
            }

            var client  = new RestClient("https://maps.googleapis.com");
            var request = new RestRequest("maps/api/timezone/json", Method.GET);

            request.AddParameter("key", _apiKey);
            request.AddParameter("location", location);
            request.AddParameter("timestamp", unixTimestamp);
            request.AddParameter("sensor", "false");

            var response = client.Execute <GoogleTimezoneResponse>(request);

            lock (_cacheTimezone) { if (!_cacheTimezone.ContainsKey(key))
                                    {
                                        _cacheTimezone.Add(key, response.Data);
                                    }
            }
            return(response.Data);
        }
Esempio n. 4
0
 // ==================================================
 // Methods
 public bool Contains(IGeoLatLon point)
 {
     return(point.Latitude >= NorthWest.Latitude &&
            point.Latitude <= SouthEast.Latitude &&
            point.Longitude >= NorthWest.Longitude &&
            point.Longitude <= SouthEast.Longitude);
 }
Esempio n. 5
0
        public static double DistanceMeters(IGeoLatLon a, IGeoLatLon b)
        {
            var r1   = GeoDirection.DegreesInRadians(a.Latitude);
            var r2   = GeoDirection.DegreesInRadians(b.Latitude);
            var rLat = GeoDirection.DegreesInRadians(b.Latitude - a.Latitude);
            var rLon = GeoDirection.DegreesInRadians(b.Longitude - a.Longitude);

            var c1 = Math.Sin(rLat / 2) * Math.Sin(rLat / 2) + Math.Cos(r1) * Math.Cos(r2) * Math.Sin(rLon / 2) * Math.Sin(rLon / 2);

            return(Math.Atan2(Math.Sqrt(c1), Math.Sqrt(1 - c1)) * 2.0 * _earthRadiusMeter);
        }
        public static string ToJson(this IGeoLatLon latlon)
        {
            if (latlon == null)
            {
                return("{}");
            }

            dynamic json = new JObject();

            json.lat = latlon.Latitude;
            json.lng = latlon.Longitude;

            return(json.ToString());
        }
        /// <summary>
        /// Geocodes the given location from google
        /// </summary>
        public List <CartoPlaceInfo> LookupLocations(IGeoLatLon point)
        {
            var response = _google.RequestLocations(point);

            var list = new List <CartoPlaceInfo>();

            foreach (var result in response.Results)
            {
                var place = FindByGooglePlaceID(result.PlaceID);
                if (place == null)
                {
                    place = new CartoPlaceInfo(result);
                }
                list.Add(place);
            }

            return(list);
        }
Esempio n. 8
0
        // --------------------------------------------------
        // Timezone
        public GeoTimezoneInfo LookupTimezone(IGeoLatLon point)
        {
            var response = _google.RequestTimezone(point);

            return(GeoTimezoneInfo.ByTZID(response.TimeZoneId));
        }
Esempio n. 9
0
        // ==================================================
        // Methods

        // --------------------------------------------------
        // Elevation
        public double LookupElevation(IGeoLatLon point)
        {
            return(_google.RequestElevation(point).Elevation);
        }
Esempio n. 10
0
        public static GeoDistance BetweenPoints(IGeoLatLon a, IGeoLatLon b)
        {
            var m = GeoDistance.DistanceMeters(a, b);

            return(GeoDistance.FromMeters(m));
        }
 /// <summary>
 /// Lists all places where the bounds contains the point
 /// </summary>
 public List <CartoPlaceInfo> ListPlacesByContainingPoint(IGeoLatLon point)
 {
     return(_cache.All.Where(x => x.Bounds.Contains(point)).ToList());
 }
Esempio n. 12
0
 // --------------------------------------------------
 // Region
 public List <GeoRegionInfo> NearbyRegions(IGeoLatLon point)
 {
     return(GeoRegionInfo.ListByLocation(point).OrderBy(x => GeoDistance.BetweenPoints(x.Center, point).Meters).ToList());
 }
Esempio n. 13
0
 // ==================================================
 // Helpers
 protected string GetLocationString(IGeoLatLon point)
 {
     return(point.Latitude.ToString("0.00000000") + "," + point.Longitude.ToString("0.00000000"));
 }
 public static string GetSearchUrl(IGeoLatLon point)
 {
     return($"/place/search/?latitude={point.Latitude}&longitude={point.Longitude}");
 }
Esempio n. 15
0
 public GeoRegionInfo NearestRegion(IGeoLatLon point)
 {
     return(NearbyRegions(point).FirstOrDefault());
 }
 /// <summary>
 /// Finds the place which is nearest to the given point
 /// </summary>
 public CartoPlaceInfo NearestPlace(IGeoLatLon point)
 {
     return(_cache.All.Where(x => x.Bounds.Contains(point)).OrderBy(x => GeoDistance.BetweenPoints(point, x.Center).Meters).FirstOrDefault());
 }
Esempio n. 17
0
        // --------------------------------------------------
        // Country
        public GeoCountryInfo NearestCountry(IGeoLatLon point)
        {
            var countries = GeoCountryInfo.ListByLocation(point).OrderBy(x => GeoDistance.BetweenPoints(x.Center, point).Meters);

            return(countries.FirstOrDefault());
        }