public GeocodingLookupServiceResult Geocode(string location)
        {
            GeocodingLookupServiceResult result;
            string googleApiKey = ConfigurationManager.AppSettings["GoogleApiKey"];
            string url          = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" + HttpUtility.UrlEncode(location);

            if (googleApiKey != null)
            {
                url += "&key=" + googleApiKey;
            }
            WebRequest  request  = HttpWebRequest.Create(url);
            WebResponse response = request.GetResponse();

            using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                string      json = reader.ReadToEnd();
                var         deserializedJsonSubset = JsonConvert.DeserializeObject <GeocodeJsonStructure>(json);
                Coordinates coords = null;
                if (deserializedJsonSubset.status == "OK")
                {
                    coords = new Coordinates()
                    {
                        Latitude = deserializedJsonSubset.results[0].geometry.location.lat, Longitude = deserializedJsonSubset.results[0].geometry.location.lng
                    };
                }
                result = new GeocodingLookupServiceResult()
                {
                    Status = ParseLookupStatus(deserializedJsonSubset.status), Location = location, Coordinates = coords
                };
            }
            return(result);
        }
Пример #2
0
        private GeocodingResult getGeocoding(string location)
        {
            GeocodingResult result = this.GeocodingCache.Lookup(location);

            if (result == null)
            {
                GeocodingLookupServiceResult lookupResult = this.GeocodingLookupService.Geocode(location);
                if (lookupResult.Status == GeocodingLookupServiceResult.LookupStatus.Ok ||
                    lookupResult.Status == GeocodingLookupServiceResult.LookupStatus.ZeroResults)
                {
                    // Cache successful results (including no-such-address results).
                    this.GeocodingCache.Store(lookupResult);
                }
                result = lookupResult;
            }
            return(result);
        }