forked from maximn/google-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
146 lines (121 loc) · 4.44 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using GoogleMapsApi;
using GoogleMapsApi.Entities.Common;
using GoogleMapsApi.Entities.Directions.Request;
using GoogleMapsApi.Entities.Directions.Response;
using GoogleMapsApi.Entities.Elevation.Request;
using GoogleMapsApi.Entities.Geocoding.Request;
using GoogleMapsApi.Entities.Geocoding.Response;
using GoogleMapsApi.StaticMaps;
using GoogleMapsApi.StaticMaps.Entities;
namespace MapsApiTest
{
class Program
{
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
//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();
}
private static void PrintDirections(DirectionsResponse directions)
{
Route route = directions.Routes.First();
Leg leg = route.Legs.First();
foreach (Step step in leg.Steps)
{
Console.WriteLine(StripHTML(step.HtmlInstructions));
var localIcon = step.TransitDetails?.Lines?.Vehicle?.LocalIcon;
if (localIcon != null)
Console.WriteLine("Local sign: " + localIcon);
}
Console.WriteLine();
}
private static string StripHTML(string html)
{
return Regex.Replace(html, @"<(.|\n)*?>", string.Empty);
}
}
}