public List <Restaurante> BuscarRestaurantesProximos(List <Restaurante> restaurantes, double latitude, double longitude)
        {
            List <Restaurante> restaurantesComDistancia = new List <Restaurante>();

            if (latitude != 0 && longitude != 0)
            {
                restaurantesComDistancia = restaurantes.Where(r => r.Endereco.Latitude != 0 && r.Endereco.Longitude != 0).ToList();

                List <string> destinos = restaurantesComDistancia.Select(r => string.Concat(
                                                                             r.Endereco.Latitude.ToString().Replace(",", ".")
                                                                             , ","
                                                                             , r.Endereco.Longitude.ToString().Replace(",", ".")
                                                                             )).ToList();

                string destinosFormatado = string.Join("|", destinos);
                string origemFormatado   = string.Concat(latitude.ToString().Replace(",", ".")
                                                         , ","
                                                         , longitude.ToString().Replace(",", "."));

                var response = ChamadaApiGET("https://maps.googleapis.com",
                                             string.Concat("/maps/api/distancematrix/json?units=metric&origins=", origemFormatado,
                                                           "&destinations=", destinosFormatado, "&mode=walking&key=", apiKey));

                if (response != null)
                {
                    GetDistanceMatrixResponse distanceMatrix = JsonConvert.DeserializeObject <GetDistanceMatrixResponse>(response);
                    if (distanceMatrix.Status == "OK")
                    {
                        for (int i = 0; i < distanceMatrix.Rows[0].Elements.Count; i++)
                        {
                            if (distanceMatrix.Rows[0].Elements[i].Status == "OK")
                            {
                                restaurantesComDistancia[i].DistanciaRestaurante                   = new DistanciaRestaurante();
                                restaurantesComDistancia[i].DistanciaRestaurante.Distancia         = string.Concat(distanceMatrix.Rows[0].Elements[i].Distance.Text, " de distância");
                                restaurantesComDistancia[i].DistanciaRestaurante.DistanciaEmMetros = distanceMatrix.Rows[0].Elements[i].Distance.Value;
                            }
                        }
                    }
                }
            }

            var restaurantesSemDistancia = restaurantes.Where(rest => !restaurantesComDistancia.Select(restLatLng => restLatLng.CodRestaurante)
                                                              .Contains(rest.CodRestaurante))
                                           .ToList();

            restaurantes = restaurantesComDistancia.OrderBy(resp => resp.DistanciaRestaurante.DistanciaEmMetros).ToList();
            restaurantes.AddRange(restaurantesSemDistancia);

            return(restaurantes);
        }
Example #2
0
        public void TestGetProximityFor100Addresses()
        {
            var mapsApiClient = new Mock <IMapsAPIClient>(MockBehavior.Strict);

            _fixture = new GoogleMapsDistanceMatrixAddressProximityService(mapsApiClient.Object, _addressGeocodingService);

            var elements = new List <DistanceMatrixRowElement>();

            for (var i = 0; i < 100; i++)
            {
                elements.Add(new DistanceMatrixRowElement
                {
                    Distance = new Duration
                    {
                        Value = 9000 + i
                    },
                    Status = "OK"
                });
            }

            var response = new GetDistanceMatrixResponse
            {
                Rows = new List <DistanceMatrixRow>
                {
                    new DistanceMatrixRow
                    {
                        Elements = elements
                    }
                }
            };

            var addresses = new List <string>();

            for (var i = 0; i < 100; i++)
            {
                addresses.Add($"{900 + i} Reading Rd, Mason, OH 45040");
            }

            mapsApiClient.Setup(mocked => mocked.APIGet("/maps/api/distancematrix/json",
                                                        It.IsAny <QueryParams>(),
                                                        It.IsAny <Func <HttpWebResponse, GetDistanceMatrixResponse> >(),
                                                        It.IsAny <DateTime?>(),
                                                        0,
                                                        It.IsAny <string>(),
                                                        true,
                                                        null,
                                                        true,
                                                        null)).Returns(response);

            var result = _fixture.GetProximity("990 Reading Rd, Mason, OH 45040", addresses);

            mapsApiClient.Verify(mocked => mocked.APIGet("/maps/api/distancematrix/json",
                                                         It.IsAny <QueryParams>(),
                                                         It.IsAny <Func <HttpWebResponse, GetDistanceMatrixResponse> >(),
                                                         It.IsAny <DateTime?>(),
                                                         0,
                                                         It.IsAny <string>(),
                                                         true,
                                                         null,
                                                         true,
                                                         null),
                                 Times.Once);
            Assert.AreEqual(100, result.Count);
        }