Ref: https://github.com/maximn/google-maps
ASUS X460J /Windows 10 x64 edu/ Visual Studio 2017 Prof
1. Console mode
1.1 New Project (Console mode)
1.2 nuget Add ref GoogleMapsApi
1.3 Copy / paste / modified
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 1. Add GoogleMapAPI
// 2. Coding by https://github.com/maximn/google-maps
namespace ConsoleApp1
{
// 2.1 Add using
using GoogleMapsApi;
using GoogleMapsApi.Entities.Common;
using GoogleMapsApi.Entities.Directions.Request;
using GoogleMapsApi.Entities.Directions.Response;
using GoogleMapsApi.Entities.Geocoding.Request;
using GoogleMapsApi.Entities.Geocoding.Response;
using GoogleMapsApi.StaticMaps;
using GoogleMapsApi.StaticMaps.Entities;
class Program
{
static void Main(string[] args)
{
// 2.2 Paste code ---
//Static class use (Directions) (Can be made from static/instance class)
DirectionsRequest directionsRequest = new DirectionsRequest()
{
// 2.3 Change Address for Direction
//Origin = "NYC, 5th and 39",
//Destination = "Philladephia, Chesnut and Wallnut",
Origin = "醒吾科技大學",
Destination = "林口Outlet",
};
DirectionsResponse directions = GoogleMaps.Directions.Query(directionsRequest);
Console.WriteLine(directions);
//Instance class use (Geocode) (Can be made from static/instance class)
GeocodingRequest geocodeRequest = new GeocodingRequest()
{
// 2.4 Change for geocoding
// Address = "new york city",
Address = "粉寮路一段101號",
};
var geocodingEngine = GoogleMaps.Geocode;
GeocodingResponse geocode = geocodingEngine.Query(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<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<GoogleMapsApi.StaticMaps.Entities.Path>(){ new GoogleMapsApi.StaticMaps.Entities.Path()
{
Style = new PathStyle()
{
Color = "red"
},
Locations = path
}}
});
Console.WriteLine("Map with path: " + url);
// 2.4 Pause
Console.ReadLine();
}
}
}
1.4 Results
2. WPF
2.1 Apply Google Map API Key for
2.2 New WPF project
2.3 Add tag
<Grid>
<Image Name="image1" />
</Grid>
2.4 Add C# Coding @ Window_Loaded
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// 3. Add Console Code
// 2.2 Paste code ---
//Static class use (Directions) (Can be made from static/instance class)
DirectionsRequest directionsRequest = new DirectionsRequest()
{
// 2.3 Change Address for Direction
//Origin = "NYC, 5th and 39",
//Destination = "Philladephia, Chesnut and Wallnut",
Origin = "醒吾科技大學",
Destination = "林口Outlet",
};
DirectionsResponse directions = GoogleMaps.Directions.Query(directionsRequest);
//Console.WriteLine(directions);
//Instance class use (Geocode) (Can be made from static/instance class)
GeocodingRequest geocodeRequest = new GeocodingRequest()
{
// 2.4 Change for geocoding
// Address = "new york city",
Address = "粉寮路一段101號",
};
var geocodingEngine = GoogleMaps.Geocode;
GeocodingResponse geocode = geocodingEngine.Query(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<ILocationString> path = steps.Select(step => step.StartLocation).ToList<ILocationString>();
// also the end location of the last step
path.Add(steps.Last().EndLocation);
// 4. Choose Map center
int mid = path.Count / 2;
string url = staticMapGenerator.GenerateStaticMapURL(new StaticMapRequest(path[mid], 12, new ImageSize(800, 400))
{
Pathes = new List<GoogleMapsApi.StaticMaps.Entities.Path>(){ new GoogleMapsApi.StaticMaps.Entities.Path()
{
Style = new PathStyle()
{
Color = "red"
},
Locations = path
}},
// 5. Add ApiKey
ApiKey = "Google_Static_MAP_API_Key"
});
//Console.WriteLine("Map with path: " + url);
// 6. Add code for url map
// 7. Add using
if (url != null)
{
Uri uri = new Uri(url);
//Uri uri = new Uri("http://maps.google.com/staticmap?center=45.728220,4.830321&zoom=8&size=200x200&maptype=roadmap&key=AIzaSyDwDqtZGEPqmoMw-UQ3-WGm0gLt987C5Ko");
System.Net.HttpWebRequest httpRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(uri);
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream imageStream = httpResponse.GetResponseStream();
Bitmap buddyIcon = new Bitmap(imageStream);
httpResponse.Close();
imageStream.Close();
//---
image1.Source = ToBitmapImage(buddyIcon);
}
// 2.4 Pause
//Console.ReadLine();
}
public BitmapImage ToBitmapImage(Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
2.5 Result