diff --git a/GoogleMapsApi/StaticMaps/Entities/Path.cs b/GoogleMapsApi/StaticMaps/Entities/Path.cs index 3530dd6..e717b8c 100644 --- a/GoogleMapsApi/StaticMaps/Entities/Path.cs +++ b/GoogleMapsApi/StaticMaps/Entities/Path.cs @@ -8,5 +8,7 @@ public class Path public PathStyle Style { get; set; } public IList Locations { get; set; } + public string EncodePolyline { get; set; } = string.Empty; + } } \ No newline at end of file diff --git a/GoogleMapsApi/StaticMaps/StaticMapsEngine.cs b/GoogleMapsApi/StaticMaps/StaticMapsEngine.cs index 3aa4957..c3cb9bd 100644 --- a/GoogleMapsApi/StaticMaps/StaticMapsEngine.cs +++ b/GoogleMapsApi/StaticMaps/StaticMapsEngine.cs @@ -28,13 +28,13 @@ public string GenerateStaticMapURL(StaticMapRequest request) var parametersList = new QueryStringParametersList(); - if (!string.IsNullOrEmpty(request.ApiKey)) - { - string apiKey = request.ApiKey; - parametersList.Add("key", apiKey); - } + if (!string.IsNullOrEmpty(request.ApiKey)) + { + string apiKey = request.ApiKey; + parametersList.Add("key", apiKey); + } - if (request.Center != null) + if (request.Center != null) { ILocationString center = request.Center; @@ -48,15 +48,15 @@ public string GenerateStaticMapURL(StaticMapRequest request) parametersList.Add("zoom", request.Zoom.ToString()); } - if (request.Scale != default) - { - if (!ValidScales.Contains(request.Scale)) - { - throw new ArgumentException("Scale is invalid; must be a value of 1, 2 or 4"); - } + if (request.Scale != default) + { + if (!ValidScales.Contains(request.Scale)) + { + throw new ArgumentException("Scale is invalid; must be a value of 1, 2 or 4"); + } - parametersList.Add("scale", request.Scale.ToString()); - } + parametersList.Add("scale", request.Scale.ToString()); + } if (request.Size.Width != default || request.Size.Height != default) { @@ -312,7 +312,10 @@ public string GenerateStaticMapURL(StaticMapRequest request) string styleString = string.Join("|", pathStyleParams); - string locations = string.Join("|", path.Locations.Select(location => location.LocationString)); + string locations = string.IsNullOrEmpty(path.EncodePolyline) ? + string.Join("|", path.Locations.Select(location => location.LocationString)) + : $"enc:{path.EncodePolyline}"; + parametersList.Add("path", string.Format("{0}|{1}", styleString, locations)); } diff --git a/README.md b/README.md index fd1edfd..65dca0a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ [![Build Status](https://github.com/maximn/google-maps/actions/workflows/dotnet.yml/badge.svg)](https://github.com/maximn/google-maps/actions/workflows/dotnet.yml) [![NuGet Status](https://img.shields.io/nuget/v/GoogleMapsApi.svg)](https://www.nuget.org/packages/GoogleMapsApi/) -google-maps -=========== +# google-maps Google Maps Web Services API wrapper for .NET @@ -12,7 +11,6 @@ The web page - http://maximn.github.com/google-maps NuGet page - https://www.nuget.org/packages/GoogleMapsApi/ - **Check out my blog at http://maxondev.com** # Quickstart @@ -26,7 +24,8 @@ NEW! Now you can easily show the results on a Static Google Map! This Library is well documented and easy to use. Code sample - -``` C# + +```C# using GoogleMapsApi; using GoogleMapsApi.Entities.Common; using GoogleMapsApi.Entities.Directions.Request; @@ -36,23 +35,28 @@ using GoogleMapsApi.Entities.Geocoding.Response; using GoogleMapsApi.StaticMaps; using GoogleMapsApi.StaticMaps.Entities; +var googleAPIKey = "google api key"; //Static class use (Directions) (Can be made from static/instance class) -DirectionsRequest directionsRequest = new DirectionsRequest() +var directionsRequest = new DirectionsRequest() { Origin = "NYC, 5th and 39", Destination = "Philladephia, Chesnut and Wallnut", + IsSSL = true, + ApiKey = googleAPIKey, }; -DirectionsResponse directions = GoogleMaps.Directions.Query(directionsRequest); +var directions = await GoogleMaps.Directions.QueryAsync(directionsRequest); Console.WriteLine(directions); //Instance class use (Geocode) (Can be made from static/instance class) -GeocodingRequest geocodeRequest = new GeocodingRequest() +var geocodeRequest = new GeocodingRequest() { - Address = "new york city", + Address = "NYC, 5th and 39", + IsSSL = true, + ApiKey = googleAPIKey, }; var geocodingEngine = GoogleMaps.Geocode; -GeocodingResponse geocode = geocodingEngine.Query(geocodeRequest); +var geocode = await geocodingEngine.QueryAsync(geocodeRequest); Console.WriteLine(geocode); // Static maps API - get static map of with the path of the directions request @@ -75,6 +79,29 @@ string url = staticMapGenerator.GenerateStaticMapURL(new StaticMapRequest(new Lo }, Locations = path }} + , + IsSSL = true + , + ApiKey = googleAPIKey }); Console.WriteLine("Map with path: " + url); + +//use EncodePath +var overviewEncodePath = directions.Routes.First().OverviewPath.GetRawPointsData(); +string encodePathUrl = staticMapGenerator.GenerateStaticMapURL(new StaticMapRequest(new Location(40.38742, -74.55366), 9, new ImageSize(800, 400)) +{ + Pathes = new List(){ new GoogleMapsApi.StaticMaps.Entities.Path() + { + Style = new PathStyle() + { + Color = "red" + }, + EncodePolyline = overviewEncodePath + }} + , + IsSSL = true + , + ApiKey = googleAPIKey +}); +Console.WriteLine("Map with EncodePath: " + encodePathUrl); ```