Exemplo n.º 1
0
        private static GeocodeMatch GetGeocodeMatch(OpenStreetGeocodeMatch item)
        {
            GeocodeMatch match = new GeocodeMatch();

            match.MatchResults["CentroidPoint"] = "POINT(" + string.Join(" ", new[] { item.Longitude, item.Latitude }) + ")";
            match.MatchResults["BoundingBox"]   = new RectangleShape(item.BoundingBox[2], item.BoundingBox[1], item.BoundingBox[3], item.BoundingBox[0]).GetWellKnownText();
            match.MatchResults["City"]          = item.Address?.City;
            match.MatchResults["State"]         = item.Address?.State;
            match.MatchResults["Country"]       = item.Address?.Country;
            return(match);
        }
        public static async Task <OpenStreetGeocodeMatch> ReverseGeocodeAsync(double longitude, double latitude)
        {
            string         entryUri   = $"http://nominatim.openstreetmap.org/reverse?lat={latitude}&lon={longitude}&format=json&addressdetails=1";
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(entryUri);

            webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36";

            return(await GetResponseAsync(webRequest, stream =>
            {
                JsonSerializer serializer = new JsonSerializer();
                using (var sr = new StreamReader(stream))
                {
                    JsonTextReader reader = new JsonTextReader(sr);
                    OpenStreetGeocodeMatch addresses = serializer.Deserialize <OpenStreetGeocodeMatch>(reader);
                    return addresses;
                }
            }));
        }