コード例 #1
0
        public async Task <MapQuestResponse> Execute(BaseRequest f, CancellationToken cancellationToken = default(CancellationToken))
        {
            HttpWebRequest request = await Send(f, cancellationToken).ConfigureAwait(false);

            MapQuestResponse r = await Parse(request, cancellationToken).ConfigureAwait(false);

            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);
        }
コード例 #2
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);
        }
コード例 #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];
		}
コード例 #4
0
        private async Task <MapQuestResponse> Parse(HttpWebRequest request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

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

            try
            {
                string json;
                using (HttpWebResponse response = await request.GetResponseAsync().ConfigureAwait(false) as HttpWebResponse)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if ((int)response.StatusCode >= 300)                     //error
                    {
                        throw new Exception((int)response.StatusCode + " " + response.StatusDescription);
                    }

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

                MapQuestResponse o = json.FromJSON <MapQuestResponse>();
                if (o == null)
                {
                    throw new Exception("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 Exception((int)response.StatusCode + " " + sb.ToString());
                }
            }
        }
コード例 #5
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]);
     }
 }
コード例 #6
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));
        }
コード例 #7
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]);
     }
 }
コード例 #8
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));
        }
コード例 #9
0
        public async Task <IEnumerable <Address> > GeocodeAsync(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 = await Execute(f).ConfigureAwait(false);

            return(HandleSingleResponse(res));
        }
コード例 #10
0
        public async Task <IEnumerable <Address> > ReverseGeocodeAsync(Location location)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }

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

            return(HandleSingleResponse(res));
        }
コード例 #11
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));
        }
コード例 #12
0
        public async Task <IEnumerable <ResultItem> > GeocodeAsync(IEnumerable <string> addresses, CancellationToken cancellationToken = default(CancellationToken))
        {
            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 = await Execute(f, cancellationToken).ConfigureAwait(false);

            return(HandleBatchResponse(res));
        }
コード例 #13
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];
		}