Exemplo n.º 1
0
        /// <summary>
        /// Searches the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task&lt;IList&lt;IAddress&gt;&gt;.</returns>
        public Task <IGeoSearchResult> SearchAsync(GooglePlacesSearchRequest request)
        {
            var ub = new UrlBuilder(SearchSourceUrl);

            ub.QueryString["key"]   = _apiKey;
            ub.QueryString["query"] = request.Keyword;

            if (request.Lattitude.HasValue && request.Longitude.HasValue)
            {
                ub.QueryString["location"] = string.Format("{0},{1}", request.Lattitude, request.Longitude);
                ub.QueryString["radius"]   = request.Distance.HasValue ? request.Distance.ToString() : "10000";
            }
            else
            {
                ub.QueryString["radius"] = request.Distance.ToString();
            }

            ub.QueryString["language"]  = request.Language;
            ub.QueryString["name"]      = request.Name;
            ub.QueryString["pagetoken"] = request.PageToken;
            ub.QueryString["maxprice"]  = request.MaxPrice.ToString();
            ub.QueryString["minprice"]  = request.MinPrice.ToString();
            ub.QueryString["opennow"]   = request.OpenNow.ToString();
            //ub.QueryString["rankby"] = request.RankBy.ToString();
            ub.QueryString["types"]         = request.Types.Any() ? request.Types.Select(x => x.ToString()).Aggregate((s, s1) => s + "|" + s1) : null;
            ub.QueryString["zagatselected"] = (request.ZagatSelected).ToString();
            ub.QueryString["pagetoken"]     = request.Session;

            return(ExecuteQueryAsync(ub));
        }
        /// <summary>
        /// Searches the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task&lt;IList&lt;IAddress&gt;&gt;.</returns>
        public async Task <IGeoSearchResult> SearchAsync(IGeoSearchRequest request)
        {
            Log.Debug("Search Request: {@request}", request);

            IGeoSearchResult searchResult = null;

            if (request.Address != null)
            {
                if (!string.IsNullOrEmpty(request.Address.Source?.ObjectID))
                {
                    var address = await _wrapper.GetAddressByIdAsync(request.Address.Source.ObjectID).ConfigureAwait(false);

                    if (address != null)
                    {
                        searchResult = new GeoSearchResult();

                        searchResult.Items.Add(address);

                        return(searchResult);
                    }
                }

                // Do we have a Lat/Long?  If so, lets use that
                if (request.Address.Location?.Latitude != null)
                {
                    Log.Debug("Executing FindLocation");
                    searchResult = await _wrapper.FindLocationAsync(request.Address.Location).ConfigureAwait(false);

                    if (searchResult != null)
                    {
                        return(searchResult);
                    }
                }

                // Do we have enough of an address to try to use that -- this includs a full line 1 or a postcode that includes the "extra" details in the US?
                if (!string.IsNullOrEmpty(request.Address.Line1) || (!string.IsNullOrEmpty(request.Address.PostalCode) && request.Address.PostalCode.Length > 7))
                {
                    Log.Debug("Executing FindAddress");

                    if (searchResult == null || searchResult.Items.All(x => x == null))
                    {
                        searchResult = await _wrapper.FindAddressAsync(request.Address).ConfigureAwait(false);

                        return(searchResult);
                    }
                }
            }

            // If all else fails, lets try a general search with as much as possible
            var searchRequest = new GooglePlacesSearchRequest()
            {
                Keyword =
                    string.Format("{0} {1}", request.Name,
                                  request.Address != null
                                                        ? string.Format("{0} {1} {2} {3}", request.Address.City, request.Address.Region, request.Address.Country,
                                                                        request.Address.PostalCode)
                                                        : null).Trim(),
                Lattitude =
                    request.Address != null && request.Address.Location != null && request.Address.Location.Latitude.HasValue
                                                ? request.Address.Location.Latitude
                                                : null,
                Longitude =
                    request.Address != null && request.Address.Location != null && request.Address.Location.Longitude.HasValue
                                                ? request.Address.Location.Longitude
                                                : null,
                Types    = request.AddressTypes.Select(x => (GoogleAddressTypes)Enum.Parse(typeof(GoogleAddressTypes), x.ToString())).ToList(),
                Distance = request.Distance
            };

            if (!string.IsNullOrEmpty(searchRequest.Keyword))
            {
                Log.Debug("Executing General Search: {@searchRequest}", searchRequest);

                searchResult = await _wrapper.SearchAsync(searchRequest).ConfigureAwait(false);
            }

            return(searchResult);
        }