Пример #1
0
        /// <summary>
        /// Perform reverse geocode function (coordinates to location string).
        /// </summary>
        /// <param name="loc">The location's geocoordinates</param>
        /// <param name="radius">(Optional) result accuracy radius</param>
        /// <returns>The string description of the location</returns>
        public async Task <string> ReverseGeocode(GeoCoordinate loc, double radius = 100)
        {
            // Create the request
            var requestResult = await CURL.GET("https://reverse.geocoder.api.here.com/6.2/reversegeocode.json",
                                               new Dictionary <string, string> {
                { "app_id", apiKey.AppId },
                { "app_code", apiKey.AppCode },
                { "prox", loc.Latitude + "," + loc.Longitude + "," + radius },
                { "mode", "retrieveAddresses" },
                { "maxresults", "1" }
            });

            // Send the request and extract the location string
            try
            {
                dynamic resultObj = JsonConvert.DeserializeObject(requestResult);
                var     view      = resultObj.Response.View[0];
                var     res       = view.Result[0];
                return(res.Location.Address.Label);
            }
            catch (JsonException ex)
            {
                logger?.LogError("Reverse geocode error: " + ex.Message);
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Perform geocode function (location string to coordinates).
        /// </summary>
        /// <param name="query">The location string</param>
        /// <returns>The geocoordinates of the location</returns>
        public async Task <GeoCoordinate> Geocode(string query)
        {
            // Clean query
            query = HttpUtility.UrlEncode(query, Encoding.UTF8);

            // Create the request
            var requestResult = await CURL.GET("https://geocoder.api.here.com/6.2/geocode.json",
                                               new Dictionary <string, string> {
                { "app_id", apiKey.AppId },
                { "app_code", apiKey.AppCode },
                { "searchtext", query }
            });

            // Send the request and extract geocoordinates
            try
            {
                dynamic resultObj = JsonConvert.DeserializeObject(requestResult);
                var     view      = resultObj.Response.View;
                var     loc       = view[0].Result[0].Location.DisplayPosition;
                return(new GeoCoordinate
                {
                    Latitude = loc.Latitude,
                    Longitude = loc.Longitude
                });
            }
            catch (JsonException ex)
            {
                logger?.LogError("Geocode error: " + ex.Message);
                return(GeoCoordinate.Unknown);
            }
        }