Ejemplo n.º 1
0
        /// <summary>
        /// This is where actual source-specific geocoding happens.  It is called internal because
        /// clients use <code>Geocode</code> which sandwiches this call between pre- and post-processing.
        /// </summary>
        /// <param name="geocodeRequest">The request to be geocoded.</param>
        /// <returns>A <code>GeocodeResponse</code> describing the results of the geocode.</returns>
        protected override GeocodeResponse InternalGeocode(GeocodeRequest geocodeRequest)
        {
            const string baseGoogleGeocodeUrl = @"https://maps.googleapis.com/maps/api/geocode/xml";

            // For the address, either use the address field or the text string,
            // depending on which one has a value.  Then, add each following address
            // part, one by one, if it has a value.
            string address =
                (!String.IsNullOrEmpty(geocodeRequest.TextString) ? geocodeRequest.TextString : geocodeRequest.Address) +
                (!String.IsNullOrEmpty(geocodeRequest.City) ? (", " + geocodeRequest.City) : "") +
                (!String.IsNullOrEmpty(geocodeRequest.State) ? (", " + geocodeRequest.State) : "") +
                (!String.IsNullOrEmpty(geocodeRequest.PostalCode) ? (" " + geocodeRequest.PostalCode) : "") +
                (!String.IsNullOrEmpty(geocodeRequest.Country) ? (", " + geocodeRequest.Country) : "");

            // add oe=utf-8 here for returning valid utf-8, not iso-8859-1
            string googleGeocodeUrl = baseGoogleGeocodeUrl +
                                      "?address=" + HttpUtility.UrlEncode(address) +
                                      "&sensor=false" +
                                      "&key=" + _authKey;

            string response     = InternetSourceUtil.GetRawHTMLFromURL(googleGeocodeUrl);
            var    responseList = XMLList2GeocodeCandidates(response);

            return(new GeocodeResponse(responseList, this));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This is where actual source-specific geocoding happens.  It is called internal because
        /// clients use <code>Geocode</code> which sandwiches this call between pre- and post-processing.
        /// </summary>
        /// <param name="request">The request to be geocoded.</param>
        /// <returns>A <code>GeocodeResponse</code> describing the results of the geocode.</returns>
        protected override GeocodeResponse InternalGeocode(GeocodeRequest request)
        {
            string geocodeURL;

            if (_username.Length > 0) //authenticated access
            {
                geocodeURL = "http://" + _username + ":" + _password + "@geocoder.us/member/service/namedcsv/geocode?address=";
            }
            else //non-authenticated access
            {
                geocodeURL = "http://geocoder.us/service/namedcsv/geocode?address=";
            }

            string queryString;

            if (!string.IsNullOrEmpty(request.TextString))
            {
                queryString = request.TextString;
            }
            else
            {
                queryString = request.Address + ", " +
                              request.City + ", " + request.State + " " +
                              request.PostalCode + ", " + request.Country;
            }

            //Geocoder US doesn't understand '&'
            queryString = queryString.Replace("&", " and ");
            queryString = HttpUtility.UrlEncode(queryString);
            string response = InternetSourceUtil.GetRawTextFromURL(geocodeURL + queryString);
            IList <GeocodeCandidate> responseList = CSVList2GeocodeCandidates(response);

            return(new GeocodeResponse(responseList, this));
        }