示例#1
0
        public static string GetTravelTime(string origin, string dest = "50.0154346,36.2284612")
        {
            StringBuilder result = new StringBuilder();

            DirectionsRequest directionsRequest = new DirectionsRequest()
            {
                Origin        = origin,
                Destination   = dest,
                ApiKey        = "AIzaSyDgjuPVvAcN9RtqgFc35OhxJPXNjQ_ugPM",
                Language      = "ru",
                TravelMode    = TravelMode.Transit,
                DepartureTime = DateTime.Now.AddMinutes(10),
            };

            DirectionsResponse directions = GoogleMaps.Directions.Query(directionsRequest);
            var route = directions.Routes.First().Legs.First();
            StaticMapsEngine        staticMapGenerator = new StaticMapsEngine();
            IEnumerable <Step>      steps = route.Steps;
            IList <ILocationString> path  = steps.Select(step => step.StartLocation).ToList <ILocationString>();

            path.Add(steps.Last().EndLocation);

            string url =
                $"https://www.google.com.ua/maps/dir/'{origin}'/{dest}'";

            result.AppendLine($"Отлично, от вас до ВУЗа {route.Distance.Text}");
            result.AppendLine($"Если выйти через 10 минут, то можно добратся за {route.Duration.Text}");
            result.AppendLine($"В ВУЗе вы будете около {route.ArrivalTime.Text}");
            result.AppendLine($"Вот оптимальный маршрут для тебя {url}");
            return(result.ToString());
        }
示例#2
0
        public void TestMap(IEnumerable <Location> locations)
        {
            Console.WriteLine($"{nameof(TestMap)}:");

            var markers = new List <Marker>()
            {
                new Marker()
                {
                    Locations = locations.Cast <ILocationString>().ToList(),
                    Style     = new MarkerStyle()
                    {
                        Color = "red",
                    }
                }
            };

            var request = new StaticMapRequest(markers, new ImageSize(512, 512))
            {
                ApiKey = ApiKey,
            };

            var engine = new StaticMapsEngine()
            {
            };

            Console.Write("\tGenerating Map URL ...");
            try {
                var url = engine.GenerateStaticMapURL(request);
                Console.Write(url);
            } catch (Exception ex) {
                Console.Write($"\t{ex.GetType()} {ex.Message}");
            }

            Console.WriteLine();
        }
示例#3
0
        public void ImageSize()
        {
            var    request        = new StaticMapRequest(new Location(0, 0), 1, new ImageSize(400, 50));
            string expectedResult = "http://maps.google.com/maps/api/staticmap" +
                                    "?center=0%2C0&zoom=1&size=400x50&sensor=false";

            string generateStaticMapURL = new StaticMapsEngine().GenerateStaticMapURL(request);

            Assert.AreEqual(expectedResult, generateStaticMapURL);
        }
示例#4
0
        public void ZoomLevels()
        {
            var    request        = new StaticMapRequest(new Location(40.714728, -73.998672), 12, new ImageSize(400, 400));
            string expectedResult = "http://maps.google.com/maps/api/staticmap" +
                                    "?center=40.714728%2C-73.998672&zoom=12&size=400x400&sensor=false";

            string generateStaticMapURL = new StaticMapsEngine().GenerateStaticMapURL(request);

            Assert.AreEqual(expectedResult, generateStaticMapURL);
        }
示例#5
0
        public void AddressTest()
        {
            var    request        = new StaticMapRequest(new AddressLocation("Berkeley,CA"), 14, new ImageSize(400, 400));
            string expectedResult = "http://maps.google.com/maps/api/staticmap" +
                                    "?center=Berkeley%2CCA&zoom=14&size=400x400&sensor=false";

            string generateStaticMapURL = new StaticMapsEngine().GenerateStaticMapURL(request);

            Assert.AreEqual(expectedResult, generateStaticMapURL);
        }
示例#6
0
        public void MapTypes()
        {
            var request = new StaticMapRequest(new Location(40.714728, -73.998672), 12, new ImageSize(400, 400));

            request.MapType = MapType.Terrain;
            string expectedResult = @"http://maps.google.com/maps/api/staticmap" +
                                    @"?center=40.714728%2C-73.998672&zoom=12&size=400x400&maptype=terrain&sensor=false";

            string actualResult = new StaticMapsEngine().GenerateStaticMapURL(request);

            Assert.AreEqual(expectedResult, actualResult);
        }
示例#7
0
        public void BasicTest()
        {
            // downtown New York City
            StaticMapRequest request = new StaticMapRequest(
                new AddressLocation("Brooklyn Bridge,New York,NY"), 14, new ImageSize(512, 512))
            {
                MapType = MapType.Roadmap,
                Markers =
                    new List <Marker>
                {
                    new Marker
                    {
                        Style = new MarkerStyle {
                            Color = "blue", Label = "S"
                        },
                        Locations = new List <ILocationString> {
                            new Location(40.702147, -74.015794)
                        }
                    },
                    new Marker
                    {
                        Style = new MarkerStyle {
                            Color = "green", Label = "G"
                        },
                        Locations = new List <ILocationString> {
                            new Location(40.711614, -74.012318)
                        }
                    },
                    new Marker
                    {
                        Style = new MarkerStyle {
                            Color = "red", Label = "C"
                        },
                        Locations = new List <ILocationString> {
                            new Location(40.718217, -73.998284)
                        }
                    }
                },
                Sensor = false
            };
            string expectedResult = "http://maps.google.com/maps/api/staticmap" +
                                    "?center=Brooklyn%20Bridge%2CNew%20York%2CNY&zoom=14&size=512x512&maptype=roadmap" +
                                    "&markers=color%3Ablue%7Clabel%3AS%7C40.702147%2C-74.015794&markers=color%3Agreen%7Clabel%3AG%7C40.711614%2C-74.012318" +
                                    "&markers=color%3Ared%7Clabel%3AC%7C40.718217%2C-73.998284&sensor=false";

            string generateStaticMapURL = new StaticMapsEngine().GenerateStaticMapURL(request);

            Assert.AreEqual(expectedResult, generateStaticMapURL);
        }
示例#8
0
 public void Init()
 {
     staticMapGenerator = new StaticMapsEngine();
 }
示例#9
0
        static void Main(string[] args)
        {
            // Driving directions
            var drivingDirectionRequest = new DirectionsRequest
            {
                Origin      = "NYC, 5th and 39",
                Destination = "Philladephia, Chesnut and Wallnut"
            };

            DirectionsResponse drivingDirections = GoogleMaps.Directions.Query(drivingDirectionRequest);

            PrintDirections(drivingDirections);

            // Transit directions
            var transitDirectionRequest = new DirectionsRequest
            {
                Origin        = "New York",
                Destination   = "Queens",
                TravelMode    = TravelMode.Transit,
                DepartureTime = DateTime.Now
            };

            DirectionsResponse transitDirections = GoogleMaps.Directions.Query(transitDirectionRequest);

            PrintDirections(transitDirections);


            var dep_time = DateTime.Today
                           .AddDays(1)
                           .AddHours(13);

            var request = new DirectionsRequest
            {
                Origin        = "T-centralen, Stockholm, Sverige",
                Destination   = "Kungsträdgården, Stockholm, Sverige",
                TravelMode    = TravelMode.Transit,
                DepartureTime = dep_time,
                Language      = "sv"
            };

            DirectionsResponse result = GoogleMaps.Directions.Query(request);

            PrintDirections(result);

            // Geocode
            var geocodeRequest = new GeocodingRequest
            {
                Address = "new york city",
            };

            GeocodingResponse geocode = GoogleMaps.Geocode.Query(geocodeRequest);

            Console.WriteLine(geocode);

            // Static maps API - get static map of with the path of the directions request
            var staticMapGenerator = new StaticMapsEngine();

            //Path from previous directions request
            IEnumerable <Step> steps = drivingDirections.Routes.First().Legs.First().Steps;
            // All start locations
            IList <ILocationString> path = steps.Select(step => step.StartLocation).ToList <ILocationString>();

            // also the end location of the last step
            path.Add(steps.Last().EndLocation);

            string url = staticMapGenerator.GenerateStaticMapURL(new StaticMapRequest(new Location(40.38742, -74.55366), 9, new ImageSize(800, 400))
            {
                Pathes = new List <Path> {
                    new Path
                    {
                        Style = new PathStyle
                        {
                            Color = "red"
                        },
                        Locations = path
                    }
                }
            });

            Console.WriteLine("Map with path: " + url);

            // Async! (Elevation)
            var elevationRequest = new ElevationRequest
            {
                Locations = new[] { new Location(54, 78) },
            };

            var task = GoogleMaps.Elevation.QueryAsync(elevationRequest)
                       .ContinueWith(t => Console.WriteLine("\n" + t.Result));

            Console.Write("Asynchronous query sent, waiting for a reply..");

            while (!task.IsCompleted)
            {
                Console.Write('.');
                Thread.Sleep(1000);
            }

            Console.WriteLine("Finished! Press any key to exit...");
            Console.ReadKey();
        }
示例#10
0
        static void Main(string[] args)
        {
            // Driving directions
            //var drivingDirectionRequest = new DirectionsRequest
            //{
            //	Origin = "NYC, 5th and 39",
            //	Destination = "Philladephia, Chesnut and Wallnut"
            //};

            var drivingDirectionRequest = new DirectionsRequest
            {
                TravelMode = TravelMode.Walking,
                Origin     = "Ha-Va'ad ha-Le'umi 21, JERUSALEM, ISRAEL",
                //Destination = "kfar ivri ,10, Jerusalem,israel"
                Destination = "31.8414894,35.2471631"
            };
            DirectionsResponse drivingDirections = GoogleMaps.Directions.Query(drivingDirectionRequest);
            Leg leg = PrintDirections(drivingDirections);

            Console.WriteLine("**********");
            Console.WriteLine(leg.Distance.Text);
            Console.WriteLine(leg.Duration.Text);
            Console.WriteLine("*****************");

            drivingDirectionRequest.TravelMode = TravelMode.Driving;
            DirectionsResponse drivingDirections2 = GoogleMaps.Directions.Query(drivingDirectionRequest);
            Leg leg2 = PrintDirections(drivingDirections2);

            Console.WriteLine("**********");
            Console.WriteLine(leg2.Distance.Text);
            Console.WriteLine(leg2.Duration.Text);
            Console.WriteLine("*****************");

            Console.ReadKey();


            // Transit directions
            var transitDirectionRequest = new DirectionsRequest
            {
                Origin        = "New York",
                Destination   = "Queens",
                TravelMode    = TravelMode.Transit,
                DepartureTime = DateTime.Now
            };

            DirectionsResponse transitDirections = GoogleMaps.Directions.Query(transitDirectionRequest);

            PrintDirections(transitDirections);


            var dep_time = DateTime.Today
                           .AddDays(1)
                           .AddHours(13);

            var request = new DirectionsRequest
            {
                Origin        = "T-centralen, Stockholm, Sverige",
                Destination   = "Kungsträdgården, Stockholm, Sverige",
                TravelMode    = TravelMode.Transit,
                DepartureTime = dep_time,
                Language      = "sv"
            };

            DirectionsResponse result = GoogleMaps.Directions.Query(request);

            PrintDirections(result);

            // Geocode
            //https://maps.googleapis.com/maps/api/geocode/json?address=Parque+Marechal+Mascarenhas+de+Morais&components=locality:Porto%20Aelgre|administrative_area:RS|country:BR
            var geocodeRequest = new GeocodingRequest
            {
                Address    = "Parque Marechal Mascarenhas de Morais",
                Components = new GeocodingComponents()
                {
                    Locality           = "Porto Alegre",
                    AdministrativeArea = "RS",
                    Country            = "BR"
                }
            };

            GeocodingResponse geocode = GoogleMaps.Geocode.Query(geocodeRequest);

            Console.WriteLine(geocode);

            // Static maps API - get static map of with the path of the directions request
            var staticMapGenerator = new StaticMapsEngine();

            //Path from previous directions request
            IEnumerable <Step> steps = drivingDirections.Routes.First().Legs.First().Steps;
            // All start locations
            IList <ILocationString> path = steps.Select(step => step.StartLocation).ToList <ILocationString>();

            // also the end location of the last step
            path.Add(steps.Last().EndLocation);

            string url = staticMapGenerator.GenerateStaticMapURL(new StaticMapRequest(new Location(40.38742, -74.55366), 9, new ImageSize(800, 400))
            {
                Pathes = new List <Path> {
                    new Path
                    {
                        Style = new PathStyle
                        {
                            Color = "red"
                        },
                        Locations = path
                    }
                },
                ApiKey = string.Empty //Pass the API Key here. if it is passed as non empty value, then it will be appended in request URL
            });

            Console.WriteLine("Map with path: " + url);

            // Async! (Elevation)
            var elevationRequest = new ElevationRequest
            {
                Locations = new[] { new Location(54, 78) },
            };

            var task = GoogleMaps.Elevation.QueryAsync(elevationRequest)
                       .ContinueWith(t => Console.WriteLine("\n" + t.Result));

            Console.Write("Asynchronous query sent, waiting for a reply..");

            while (!task.IsCompleted)
            {
                Console.Write('.');
                Thread.Sleep(1000);
            }

            Console.WriteLine("Finished! Press any key to exit...");
            Console.ReadKey();
        }
示例#11
0
        static void Main(string[] args)
        {
            //Static class use (Directions)
            DirectionsRequest directionsRequest = new DirectionsRequest()
            {
                Origin      = "NYC, 5th and 39",
                Destination = "Philladephia, Chesnut and Wallnut",
            };

            DirectionsResponse directions = MapsAPI.GetDirections(directionsRequest);

            Console.WriteLine(directions);


            //Instance class use (Geocode)
            GeocodingRequest geocodeRequest = new GeocodingRequest()
            {
                Address = "new york city",
            };

            GeocodingEngine geocodingEngine = new GeocodingEngine();

            GeocodingResponse geocode = geocodingEngine.GetGeocode(geocodeRequest);

            Console.WriteLine(geocode);


            // Static maps API - get static map of with the path of the directions request
            StaticMapsEngine staticMapGenerator = new StaticMapsEngine();

            //Path from previos directions request
            IEnumerable <Step> steps = directions.Routes.First().Legs.First().Steps;
            // All start locations
            IList <ILocation> path = steps.Select(step => step.StartLocation).ToList <ILocation>();

            // also the end location of the last step
            path.Add(steps.Last().EndLocation);

            string url = staticMapGenerator.GenerateStaticMapURL(new StaticMapRequest(new Location(40.38742, -74.55366), 9, new ImageSize(800, 400))
            {
                Pathes = new List <Path>()
                {
                    new Path()
                    {
                        Style = new PathStyle()
                        {
                            Color = "red"
                        },
                        Locations = path
                    }
                }
            });

            Console.WriteLine("Map with path: " + url);



            //Instance class - Async! (Elevation)
            ElevationRequest elevationRequest = new ElevationRequest()
            {
                Locations = new Location[] { new Location(54, 78) },
            };

            ElevationEngine elevationEngine = new ElevationEngine();

            elevationEngine.BeginGetElevation(elevationRequest,
                                              ar =>
            {
                ElevationResponse elevation = elevationEngine.EndGetElevation(ar);
                Console.WriteLine(elevation);
            },
                                              null);

            Console.WriteLine("Finised! (But wait .. async elevation request should get response soon)");



            Console.ReadKey();
        }