public void ReturnsNearbySearchRequest()
        {
            var request = new PlacesNearByRequest
            {
                ApiKey   = ApiKey,
                Keyword  = "pizza",
                Radius   = 10000,
                Location = new Location(47.611162, -122.337644), //Seattle, Washington, USA
            };

            PlacesNearByResponse result = GoogleMaps.PlacesNearBy.Query(request);

            AssertInconclusive.NotExceedQuota(result);
            Assert.AreEqual(Status.OK, result.Status);
            Assert.IsTrue(result.Results.Count() > 5);
        }
        public void TestNearbySearchType()
        {
            var request = new PlacesNearByRequest
            {
                ApiKey   = ApiKey,
                Radius   = 10000,
                Location = new Location(40.6782552, -73.8671761), // New York
                Type     = "airport",
            };

            PlacesNearByResponse result = GoogleMaps.PlacesNearBy.Query(request);

            AssertInconclusive.NotExceedQuota(result);
            Assert.AreEqual(Status.OK, result.Status);
            Assert.IsTrue(result.Results.Any());
            Assert.IsTrue(result.Results.Any(t => t.Name.Contains("John F. Kennedy")));
        }
        public void TestNearbySearchPagination()
        {
            var request = new PlacesNearByRequest
            {
                ApiKey   = ApiKey,
                Keyword  = "pizza",
                Radius   = 10000,
                Location = new Location(47.611162, -122.337644), //Seattle, Washington, USA
                Sensor   = false,
            };

            PlacesNearByResponse result = GoogleMaps.PlacesNearBy.Query(request);

            if (result.Status == GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OVER_QUERY_LIMIT)
            {
                Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
            }
            Assert.AreEqual(GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OK, result.Status);
            //we should have more than one page of pizza results from the NearBy Search
            Assert.IsTrue(!String.IsNullOrEmpty(result.NextPage));
            //a full page of results is always 20
            Assert.IsTrue(result.Results.Count() == 20);
            var resultFromFirstPage = result.Results.FirstOrDefault(); //hold onto this

            //get the second page of results. Delay request by 2 seconds
            //Google API requires a short processing window to develop the second page. See Google API docs for more info on delay.

            Thread.Sleep(2000);
            request = new PlacesNearByRequest
            {
                ApiKey    = ApiKey,
                Keyword   = "pizza",
                Radius    = 10000,
                Location  = new Location(47.611162, -122.337644), //Seattle, Washington, USA
                Sensor    = false,
                PageToken = result.NextPage
            };
            result = GoogleMaps.PlacesNearBy.Query(request);
            Assert.AreEqual(GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OK, result.Status);
            //make sure the second page has some results
            Assert.IsTrue(result.Results != null && result.Results.Count() > 0);
            //make sure the result from the first page isn't on the second page to confirm we actually got a second page with new results
            Assert.IsFalse(result.Results.Any(t => t.Reference == resultFromFirstPage.Reference));
        }
Beispiel #4
0
        public void ReturnsNearbySearchRequest()
        {
            var request = new PlacesNearByRequest
            {
                ApiKey   = ApiKey,
                Keyword  = "pizza",
                Radius   = 10000,
                Location = new Location(47.611162, -122.337644), //Seattle, Washington, USA
            };

            PlacesNearByResponse result = GoogleMaps.PlacesNearBy.Query(request);

            if (result.Status == GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OVER_QUERY_LIMIT)
            {
                Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
            }
            Assert.AreEqual(GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OK, result.Status);
            Assert.IsTrue(result.Results.Count() > 5);
        }
        public async Task TestNearbySearchPaginationAsync()
        {
            var request = new PlacesNearByRequest
            {
                ApiKey   = ApiKey,
                Keyword  = "pizza",
                Radius   = 10000,
                Location = new Location(47.611162, -122.337644), //Seattle, Washington, USA
            };

            PlacesNearByResponse result = await GoogleMaps.PlacesNearBy.QueryAsync(request);

            AssertInconclusive.NotExceedQuota(result);
            Assert.AreEqual(Status.OK, result.Status);
            //we should have more than one page of pizza results from the NearBy Search
            Assert.IsTrue(!String.IsNullOrEmpty(result.NextPage));
            //a full page of results is always 20
            Assert.IsTrue(result.Results.Count() == 20);
            var resultFromFirstPage = result.Results.FirstOrDefault(); //hold onto this

            //get the second page of results. Delay request by 2 seconds
            //Google API requires a short processing window to develop the second page. See Google API docs for more info on delay.

            Thread.Sleep(2000);
            request = new PlacesNearByRequest
            {
                ApiKey    = ApiKey,
                Keyword   = "pizza",
                Radius    = 10000,
                Location  = new Location(47.611162, -122.337644), //Seattle, Washington, USA
                PageToken = result.NextPage
            };
            result = await GoogleMaps.PlacesNearBy.QueryAsync(request);

            Assert.AreEqual(GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OK, result.Status);
            //make sure the second page has some results
            Assert.IsTrue(result.Results != null && result.Results.Any());
            //make sure the result from the first page isn't on the second page to confirm we actually got a second page with new results
            Assert.IsFalse(result.Results.Any(t => resultFromFirstPage != null && t.PlaceId == resultFromFirstPage.PlaceId));
        }
Beispiel #6
0
        public void TestNearbySearchType()
        {
            var request = new PlacesNearByRequest
            {
                ApiKey   = ApiKey,
                Radius   = 10000,
                Location = new Location(64.6247243, 21.0747553), // Skellefteå, Sweden
                Type     = "airport",
            };

            PlacesNearByResponse result = GoogleMaps.PlacesNearBy.Query(request);

            if (result.Status == GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OVER_QUERY_LIMIT)
            {
                Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
            }
            Assert.AreEqual(GoogleMapsApi.Entities.PlacesNearBy.Response.Status.OK, result.Status);
            Assert.IsTrue(result.Results.Any());
            var correctAirport = result.Results.Where(t => t.Name.Contains("Skellefte"));

            Assert.IsTrue(correctAirport != null && correctAirport.Any());
        }
Beispiel #7
0
        public async Task <((decimal?lat, decimal?lon) location, string gym, string distance)> ProcessGym(Raid raid, StringBuilder description, int?precision = null, MidpointRounding?rounding = null, CancellationToken cancellationToken = default)
        {
            var    location = (lat : raid.Lat, lon : raid.Lon);
            string distance = default;
            var    gym      = raid.Gym ?? raid.PossibleGym;

            if (gym == null)
            {
                raid.PossibleGym = await myDbContext.Set <Raid>()
                                   .FindKnownGym((decimal)raid.Lat, (decimal)raid.Lon, precision, rounding)
                                   .Select(_ => _.Gym ?? _.PossibleGym)
                                   .FirstOrDefaultAsync(cancellationToken);
            }

            //if (!string.IsNullOrEmpty(gym))
            //{
            //  description.Append(gym);
            //}

            //var geoCode = await myGeoCoder.Decode(lon, lat, cancellationToken: cancellationToken,
            //    parameters: new Dictionary<string, string>
            //    {
            //        {"results", "1"},
            //        {"kind", "metro"}
            //    });
            //var metro = geoCode?.featureMember?.FirstOrDefault()?.GeoObject;

            try
            {
                var destination = new Location((double)location.lat, (double)location.lon);
                var geoRequest  = new PlacesNearByRequest
                {
                    Location = destination,
                    Type     = "subway_station",
                    RankBy   = RankBy.Distance,
                    ApiKey   = myGeoCoderOptions.GoogleKey
                };

                var geoResponse = await GoogleMaps.PlacesNearBy.QueryAsync(geoRequest, myTelemetryClient, cancellationToken);

                Result foundAddress = null;
                Action <StringBuilder> postProcessor = null;
                foreach (var address in geoResponse.Results)
                {
                    if (address.Types.Contains("subway_station"))
                    {
                        var distanceMatrixRequest = new DistanceMatrixRequest
                        {
                            Origins      = new[] { $"place_id:{address.PlaceId}" },
                            Destinations = new[] { destination.LocationString },
                            Mode         = DistanceMatrixTravelModes.walking,
                            ApiKey       = myGeoCoderOptions.GoogleKey
                        };
                        var distanceMatrixResponse = await GoogleMaps.DistanceMatrix.QueryAsync(distanceMatrixRequest, myTelemetryClient, cancellationToken);

                        var distanceElement = distanceMatrixResponse.Rows.FirstOrDefault()?.Elements.FirstOrDefault();

                        if (distanceElement?.Distance.Value <= myGeoCoderOptions.MaxDistanceToMetro)
                        {
                            foundAddress  = address;
                            postProcessor = descr => descr.Sanitize(distance = $" ∙ {distanceElement.Distance.Text} ∙ {distanceElement.Duration.Text}");
                            break;
                        }
                    }
                }

                string placeId;
                string name;
                if (foundAddress == null)
                {
                    var geoCodingRequest = new GeocodingRequest
                    {
                        Location = destination,
                        ApiKey   = myGeoCoderOptions.GoogleKey
                    };
                    var geoCodingResults = (await GoogleMaps.Geocode.QueryAsync(geoCodingRequest, myTelemetryClient, cancellationToken)).Results;
                    var result           = geoCodingResults.FirstOrDefault(_ => _.Types.Contains("locality"));
                    placeId = result?.PlaceId;
                    name    = result?.FormattedAddress;
                }
                else
                {
                    placeId = foundAddress.PlaceId;
                    name    = foundAddress.Name;
                }

                raid.NearByPlaceId = placeId;
                raid.NearByAddress = name;

                if (!string.IsNullOrEmpty(name))
                {
                    description
                    .Append(description.Length > 0 ? " ∙ " : "")
                    .Append(name);

                    postProcessor?.Invoke(description);
                }
            }
            catch (Exception ex)
            {
                myTelemetryClient.TrackExceptionEx(ex);
            }

            return(location, gym, distance);
        }
        private async Task <List <PositionModel> > GetNearByPlaces(List <PositionModel> curlist, double lat, double lng, string pageToken = "", int curItemCount = 0, int radius = 500)
        {
            //return new List<PositionModel>()
            //{
            //        new PositionModel(10.78761, 106.6987),
            //    new PositionModel(10.78739,106.69848),
            //    new PositionModel(10.78698,106.69809),
            //    new PositionModel(10.78646,106.69762),
            //    new PositionModel(10.78646,106.69869),
            //    new PositionModel(10.78551,106.69867),
            //    new PositionModel(10.78487,106.69932),
            //    new PositionModel(10.78454,106.69966),
            //    new PositionModel(10.78454,106.69972),
            //    new PositionModel(10.78506,106.70013),
            //    new PositionModel(10.78509,106.70016),
            //    new PositionModel(10.78528,106.70034),
            //    new PositionModel(10.78544,106.7005),
            //    new PositionModel(10.78581,106.70084),
            //    new PositionModel(10.78604,106.70109),
            //    new PositionModel(10.78644,106.70149),
            //    new PositionModel(10.78644,106.70182),
            //    new PositionModel(10.78654,106.70162),
            //    new PositionModel(10.78659,106.70157),
            //    new PositionModel(10.78784,106.70023)
            //};
            PlacesNearByRequest request = new PlacesNearByRequest();
            string appSetting           = ConfigurationManager.AppSettings["GoogleApiKey"];

            request.ApiKey = appSetting;
            Location location = new Location(lat, lng);

            request.Location = location;
            double?nullable = radius;

            request.Radius = nullable;
            string str = pageToken;

            request.PageToken = str;
            PlacesNearByResponse placesNearByResponse = await GoogleMaps.PlacesNearBy.QueryAsync(request);

            PlacesNearByResponse response = placesNearByResponse;
            List <PositionModel> result   = new List <PositionModel>();
            int triedNumber = 0;

            if (response.Status == Status.OK)
            {
                foreach (Result result1 in response.Results)
                {
                    Result item = result1;
                    if (!string.IsNullOrEmpty(item.Vicinity))
                    {
                        PositionModel pos = new PositionModel()
                        {
                            Lat = item.Geometry.Location.Latitude, Lng = item.Geometry.Location.Longitude, Address = item.Vicinity
                        };
                        result.Add(pos);
                        curlist.Add(pos);
                    }
                }
                if (!string.IsNullOrEmpty(response.NextPage) && curItemCount < 100)
                {
                    List <PositionModel> positionModelList = await GetNearByPlaces(curlist, lat, lng, response.NextPage, curItemCount);

                    List <PositionModel> nextList = positionModelList;
                    if (nextList == null)
                    {
                        return(null);
                    }
                    result.AddRange(nextList);
                    curlist.AddRange(nextList);
                }
            }
            else
            {
                if (response.Status == Status.OVER_QUERY_LIMIT || response.Status == Status.REQUEST_DENIED || response.Status == Status.ZERO_RESULTS)
                {
                    return(null);
                }
                if (triedNumber < 3)
                {
                    List <PositionModel> positionModelList = await GetNearByPlaces(curlist, lat, lng, pageToken, curItemCount);

                    List <PositionModel> nextList = positionModelList;
                    if (nextList == null)
                    {
                        return(null);
                    }
                    result.AddRange(nextList);
                    curlist.AddRange(nextList);
                }
            }
            return(result);
        }
        public static async Task <PlacesNearByResponse> QueryAsync(this IEngineFacade <PlacesNearByRequest, PlacesNearByResponse> engine, PlacesNearByRequest request, TelemetryClient telemetryClient, CancellationToken cancellationToken = default)
        {
            request.Language = CultureInfo.CurrentUICulture.IetfLanguageTag;
            return(await QueryAsync(engine, request, telemetryClient, (telemetry, response) =>
            {
                telemetry.Name = nameof(GoogleMaps.PlacesNearBy);
                switch (response.Status)
                {
                case Status.OK:
                case Status.ZERO_RESULTS:
                    telemetry.Success = true;
                    break;

                default:
                    telemetry.Success = false;
                    break;
                }
                telemetry.ResultCode = response.Status.ToString();
            }, cancellationToken));
        }