Esempio n. 1
0
        public MapQuestResponse Execute(BaseRequest f)
        {
            HttpWebRequest   request = Send(f);
            MapQuestResponse r       = Parse(request);

            if (r != null && !r.Results.IsNullOrEmpty())
            {
                foreach (MapQuestResult o in r.Results)
                {
                    if (o == null)
                    {
                        continue;
                    }

                    foreach (MapQuestLocation l in o.Locations)
                    {
                        if (!string.IsNullOrWhiteSpace(l.FormattedAddress) || o.ProvidedLocation == null)
                        {
                            continue;
                        }

                        if (string.Compare(o.ProvidedLocation.FormattedAddress, "unknown", true) != 0)
                        {
                            l.FormattedAddress = o.ProvidedLocation.FormattedAddress;
                        }
                        else
                        {
                            l.FormattedAddress = o.ProvidedLocation.ToString();
                        }
                    }
                }
            }
            return(r);
        }
Esempio n. 2
0
        MapQuestResponse Parse(HttpWebRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            string requestInfo = string.Format("[{0}] {1}", request.Method, request.RequestUri);

            try
            {
                string json;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if ((int)response.StatusCode >= 300)                     //error
                    {
                        throw new WebException(response.StatusDescription, (WebExceptionStatus)response.StatusCode);
                    }

                    using (var sr = new StreamReader(response.GetResponseStream()))
                        json = sr.ReadToEnd();
                }
                if (string.IsNullOrWhiteSpace(json))
                {
                    throw new ApplicationException("Remote system response with blank: " + requestInfo);
                }

                MapQuestResponse o = json.FromJSON <MapQuestResponse>();
                if (o == null)
                {
                    throw new ApplicationException("Unable to deserialize remote response: " + requestInfo + " => " + json);
                }

                return(o);
            }
            catch (WebException wex)             //convert to simple exception & close the response stream
            {
                using (HttpWebResponse response = wex.Response as HttpWebResponse)
                {
                    var sb = new StringBuilder(requestInfo);
                    sb.Append(" | ");
                    sb.Append(response.StatusDescription);
                    sb.Append(" | ");
                    using (var sr = new StreamReader(response.GetResponseStream()))
                    {
                        sb.Append(sr.ReadToEnd());
                    }
                    throw new WebException(sb.ToString(), (WebExceptionStatus)response.StatusCode);
                }
            }
        }
Esempio n. 3
0
 IEnumerable <Address> HandleSingleResponse(MapQuestResponse res)
 {
     if (res != null && !res.Results.IsNullOrEmpty())
     {
         return(HandleSingleResponse(from r in res.Results
                                     where r != null && !r.Locations.IsNullOrEmpty()
                                     from l in r.Locations
                                     select l));
     }
     else
     {
         return(new Address[0]);
     }
 }
Esempio n. 4
0
        public IEnumerable <Address> Geocode(string address)
        {
            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentException("address can not be null or empty!");
            }

            var f = new GeocodeRequest(key, address)
            {
                UseOSM = this.UseOSM
            };
            MapQuestResponse res = Execute(f);

            return(HandleSingleResponse(res));
        }
Esempio n. 5
0
 ICollection <ResultItem> HandleBatchResponse(MapQuestResponse res)
 {
     if (res != null && !res.Results.IsNullOrEmpty())
     {
         return((from r in res.Results
                 where r != null && !r.Locations.IsNullOrEmpty()
                 let resp = HandleSingleResponse(r.Locations)
                            where resp != null
                            select new ResultItem(r.ProvidedLocation, resp)).ToArray());
     }
     else
     {
         return(new ResultItem[0]);
     }
 }
Esempio n. 6
0
        public IEnumerable <Address> ReverseGeocode(Location location)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }

            var f = new ReverseGeocodeRequest(key, location)
            {
                UseOSM = this.UseOSM
            };
            MapQuestResponse res = Execute(f);

            return(HandleSingleResponse(res));
        }
Esempio n. 7
0
        public IEnumerable <ResultItem> Geocode(IEnumerable <string> addresses)
        {
            if (addresses == null)
            {
                throw new ArgumentNullException("addresses");
            }

            string[] adr = (from a in addresses
                            where !string.IsNullOrWhiteSpace(a)
                            group a by a into ag
                            select ag.Key).ToArray();
            if (adr.IsNullOrEmpty())
            {
                throw new ArgumentException("Atleast one none blank item is required in addresses");
            }

            var f = new BatchGeocodeRequest(key, adr)
            {
                UseOSM = this.UseOSM
            };
            MapQuestResponse res = Execute(f);

            return(HandleBatchResponse(res));
        }