示例#1
0
        public static int GetDistance(List <string> locations)
        {
            int distance = 0;

            GoogleSigned.AssignAllServices(new GoogleSigned(apiKey));

            var request = new DistanceMatrixRequest();

            foreach (string s in locations)
            {
                request.AddOrigin(new Google.Maps.Location(s));
                request.AddDestination(new Google.Maps.Location(s));
            }
            request.Units = Units.imperial;

            var response = new DistanceMatrixService().GetResponse(request);

            if (response.Status == ServiceResponseStatus.Ok && response.Rows.Count() > 0)
            {
                for (int i = 0; i < response.Rows.Length - 1; i++)
                {
                    distance += (int)response.Rows[i].Elements[i + 1].distance.Value;
                }
            }

            return(distance);
        }
示例#2
0
文件: APIGoogle.cs 项目: paneee/SWay
        public static DistanceMatrixResponse DistanceMatrix(DistanceMatrixRequest request)
        {
            if (request.WaypointsDestination.Count < 10)
            {
                DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);
                return(response);
            }
            else
            {
                List <DistanceMatrixResponse> tempList = new List <DistanceMatrixResponse>();

                DirectionService  ds = new DirectionService();
                DirectionRequest  dr;
                DirectionResponse dre;

                DistanceMatrixRequest  _request;
                DistanceMatrixResponse _response;

                for (int i = 0; i < request.WaypointsDestination.Count; i++)
                {
                    for (int j = 0; j < request.WaypointsDestination.Count; j++)
                    {
                        //_request = new DistanceMatrixRequest()
                        //{
                        //    WaypointsOrigin = new List<Location>() { request.WaypointsOrigin[i], request.WaypointsOrigin[j] },
                        //    WaypointsDestination = new List<Location> { request.WaypointsDestination[i], request.WaypointsDestination[j] },
                        //    Mode = TravelMode.driving,
                        //    Units = Units.metric
                        //};

                        //_response = new DistanceMatrixService().GetResponse(_request);
                        //tempList.Add(_response);

                        dr = new DirectionRequest()
                        {
                            Origin      = new Location("Kraków"),
                            Destination = new Location("Limanowa")
                        };

                        //DirectionRequest directionsRequest = new DirectionRequest()
                        //{
                        //    Origin = "Surrey, UK",
                        //    Destination = "London, UK",
                        //}

                        //DirectionResponse directionsResponse = GoogleMapsApi.GoogleMaps.Directions.Query(directionsRequest);

                        dre = ds.GetResponse(dr);
                    }
                }

                //var temp = ds.GetResponse(DirectionRequest request);
                //ds.GetResponse
            }
            //DistanceMatrixRequest request;



            return(null);
        }
示例#3
0
        public void UpdateDistanceMatrix()
        {
            var origins = new List <Location>()
            {
                homeAddress.AsLocation()
            };
            var destinations = new List <Location>()
            {
                homeAddress.AsLocation()
            };

            origins.AddRange(from customer in customersList select customer.AsLocation());
            destinations.AddRange(from customer in customersList select customer.AsLocation());

            DistanceMatrixRequest request = new DistanceMatrixRequest()
            {
                WaypointsOrigin      = origins,
                WaypointsDestination = destinations
            };
            var response = new DistanceMatrixService().GetResponse(request);

            if (response.Status == 0)
            {
                throw new Exception("Invalid address");
            }

            distanceMatrix = response.Rows;
        }
示例#4
0
        private Tuple <double?, long> GetDistanceBetweenTwoAddresses(Address addressOrigin, Address addressDestination)
        {
            DistanceMatrixRequest request = new DistanceMatrixRequest();

            GetLatirudeAndLogitudeOfAnAddress(addressOrigin);
            GetLatirudeAndLogitudeOfAnAddress(addressDestination);

            request.AddDestination(new LatLng(latitude: (decimal)addressOrigin.Latitude.Value, longitude: (decimal)addressOrigin.Longitude));

            request.AddOrigin(new LatLng(latitude: (decimal)addressDestination.Latitude.Value, longitude: (decimal)addressDestination.Longitude));

            request.Mode  = TravelMode.driving;
            request.Units = Units.metric;

            DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);

            long   duration = 0;
            double?distance = null;

            if (response.Status == ServiceResponseStatus.Ok && response.Rows.Length == 1)
            {
                duration = response.Rows[0].Elements[0].duration.Value;
                distance = response.Rows[0].Elements[0].distance.Value;
                return(Tuple.Create(distance, duration));
            }
            else if (response.Status == ServiceResponseStatus.OverQueryLimit)
            {
                Console.WriteLine("Excesso de requisições no dia!");
            }
            return(Tuple.Create(distance, duration));
        }
示例#5
0
        public TryWatchDialog(IStatePropertyAccessor <Models.UserData> userDataStateAccessor, IConfiguration configuration)
            : base(nameof(TryWatchDialog))
        {
            UserStateAccessor = userDataStateAccessor ?? throw new ArgumentNullException(nameof(userDataStateAccessor));

            // Cognitive Services
            _textAnalyticsService   = new TextAnalyticsService(configuration["textAnalyticsKey"], configuration["region"]);
            _distanceMatrixService  = new DistanceMatrixService(configuration["bingMapsApiKey"]);
            _faceRecognitionService = new FaceRecognitionService(configuration["faceRecognitionKey"], configuration["region"]);

            // Preset latitud and longitude for testing.
            _origin      = $"{configuration["userLatitude"]},{configuration["userLongitude"]}";
            _destination = $"{configuration["storeLatitude"]},{configuration["storeLongitude"]}";

            // Define Waterfallsteps for the new TryWatch dialog
            var tryWatchWaterfallSteps = new WaterfallStep[]
            {
                SearchClosestStoreStepAsync,
                CheckStoreCalendarStepAsync,
                NamePromptStepAsync,
                BookAppointmentStepAsync,
                SaveSelfieStepAsync,
            };

            AddDialog(new WaterfallDialog(TryWatchWaterfall, tryWatchWaterfallSteps));
            AddDialog(new TextPrompt(ConfirmStore));
            AddDialog(new TextPrompt(SelectStoreSchedule));
            AddDialog(new TextPrompt(NamePrompt));
            AddDialog(new AttachmentPrompt(BookAppointment));
        }
示例#6
0
        public double CalculaDistanciaEntreDoisPontos(double lat1, double long1, double lat2, double long2)
        {
            DistanceMatrixRequest request = new DistanceMatrixRequest();

            request.AddOrigin(new Waypoint {
                Position = new LatLng(lat1, long1)
            });
            request.AddDestination(new Waypoint {
                Position = new LatLng(lat2, long2)
            });
            request.Sensor = false;

            try
            {
                DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);

                if (response.Status == ServiceResponseStatus.Ok)
                {
                    int distInteger;

                    if (Int32.TryParse(response.Rows[0].Elements[0].distance.Value, out distInteger))
                    {
                        return((double)distInteger / 1000);
                    }
                }
            }
            catch { }

            return(0.0);
        }
示例#7
0
        private static DistanceMatrixResponse GoogleServiceRequest(DistanceMatrixRequest req)
        {
            Logger.Border('*');
            Logger.Debug("Initiating Google Service API request");
            DistanceMatrixService svc = new DistanceMatrixService();
            var response = svc.GetResponse(req);

            Logger.Debug("Response received for Google Service API request");
            Logger.Debug("Status: {0}", response.Status);
            Logger.Debug("Origins: {0}", response.Rows != null ? response.Rows.Count():0);
            Logger.Debug("Destinations: {0}", response.Rows != null && response.Rows.Count() > 0 ? response.Rows[0].Elements.Count() / response.Rows.Count() : 0);
            Logger.Debug("Calculations: {0}", response.Rows != null && response.Rows.Count() > 0 ? response.Rows[0].Elements.Count() : 0);
            Logger.Border('*');
            return(response);
        }
        public void DrivingDistancebyAddress()
        {
            DistanceMatrixRequest request = new DistanceMatrixRequest();

            //sheffield
            request.AddDestination(new Location("Sheffield"));
            //rotherham
            request.AddOrigin(new Location("Rotherham"));

            request.Sensor = true;
            request.Mode   = TravelMode.driving;

            DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);

            Assert.IsTrue(response.Status == ServiceResponseStatus.Ok);
        }
        public void DrivingDistancebyAddressAndLngLat()
        {
            DistanceMatrixRequest request = new DistanceMatrixRequest();

            //sheffield
            request.AddDestination(new Location("Sheffield"));
            //rotherham
            request.AddOrigin(new LatLng(latitude: 53.434297m, longitude: -1.364678m));

            request.Sensor = true;
            request.Mode   = TravelMode.driving;

            DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);

            Assert.IsTrue(response.Status == ServiceResponseStatus.Ok);
        }
        public void DrivingDistancebyLngLatHasOneOriginAndDestinationAdresses()
        {
            DistanceMatrixRequest request = new DistanceMatrixRequest();

            //rotherham
            request.AddOrigin(new LatLng(latitude: 53.434297m, longitude: -1.364678m));
            //sheffield
            request.AddDestination(new LatLng(latitude: 53.378243m, longitude: -1.462131m));

            request.Sensor = true;
            request.Mode   = TravelMode.driving;

            DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);

            Assert.IsTrue(response.Status == ServiceResponseStatus.Ok);
            Assert.IsTrue(response.DestinationAddresses.Length == 1);
            Assert.IsTrue(response.OriginAddresses.Length == 1);
        }
示例#11
0
        public IEnumerable <string> GetDistance(string origin, string destination)
        {
            try {
                var distanceMatrixRequest = new DistanceMatrixRequest();
                distanceMatrixRequest.AddOrigin(origin);
                distanceMatrixRequest.AddDestination(destination);
                distanceMatrixRequest.Mode = TravelMode.driving;

                var response = new DistanceMatrixService().GetResponse(distanceMatrixRequest);

                return(new List <string> {
                    response?.Rows[0]?.Elements[0]?.distance?.Text ?? "Failed to complete operation"
                });
            }
            catch (Exception e) {
                this.logger.Debug($"Failed to get distance between {origin} and {destination}. {e}");
                throw;
            }
        }
        private ExpandoObject DistanceByCoordinates(double pLatitude1, double pLongitude1, double pLatitude2, double pLongitude2)
        {
            DistanceMatrixRequest request = new DistanceMatrixRequest();

            request.AddDestination(new LatLng(latitude: pLatitude2, longitude: pLongitude2));
            request.AddOrigin(new LatLng(latitude: pLatitude1, longitude: pLongitude1));

            request.Mode = TravelMode.driving;

            DistanceMatrixResponse response = new DistanceMatrixService().GetResponse(request);
            dynamic data = new ExpandoObject();

            if (response.Status == ServiceResponseStatus.Ok)
            {
                data.meters     = response.Rows[0].Elements[0].distance.Value;
                data.kilometers = response.Rows[0].Elements[0].distance.Text;
                return(data);
            }
            else
            {
                return(null);
            }
        }
示例#13
0
        private void Calculate()
        {
            var req = new DistanceMatrixRequest();

            req.AddOrigin(new Waypoint()
            {
                Address = _start.ToString()
            });
            req.AddDestination(new Waypoint()
            {
                Address = _stop.ToString()
            });
            req.Sensor = false;
            req.Mode   = TravelMode.driving;
            req.Units  = Units.imperial;

            var response = new DistanceMatrixService().GetResponse(req);

            Distance = Convert.ToInt32(response.Rows.First().Elements.First().distance.Value);
            Duration = Convert.ToInt32(response.Rows.First().Elements.First().duration.Value);

            Console.WriteLine("Distance from {0}, to {1} is {2} - Travel Time: {3}", _start, _stop, Distance, Duration);
        }
示例#14
0
        public DistanceMatrixValueViewModel getDistanceValue(string origin, string destinatination)
        {
            DistanceMatrix d = DistanceMatrixService.getDistance(origin, destinatination);

            return(new DistanceMatrixValueViewModel(d.Origin, d.Destination, d.DistanceV, d.DurationV));
        }
示例#15
0
        public DistanceMatrixTextViewModel getDistanceText(string origin, string destinatination)
        {
            DistanceMatrix d = DistanceMatrixService.getDistance(origin, destinatination);

            return(new DistanceMatrixTextViewModel(d.Origin, d.Destination, d.DistanceT, d.DurationT));
        }
示例#16
0
        public DistanceMatrixTextViewModel getJussieuOtacos()
        {
            DistanceMatrix d = DistanceMatrixService.getDistance("48.8448144,2.3544458999999733", "48.8458361, 2.355213899999967");

            return(new DistanceMatrixTextViewModel(d.Origin, d.Destination, d.DistanceT, d.DurationT));
        }