Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add StaticMapsEngine.GenerateStaticMapURL accept encode path #159

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GoogleMapsApi/StaticMaps/Entities/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public class Path
public PathStyle Style { get; set; }

public IList<ILocationString> Locations { get; set; }
public string EncodePolyline { get; set; } = string.Empty;

}
}
33 changes: 18 additions & 15 deletions GoogleMapsApi/StaticMaps/StaticMapsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public string GenerateStaticMapURL(StaticMapRequest request)

var parametersList = new QueryStringParametersList();

if (!string.IsNullOrEmpty(request.ApiKey))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional? If not, please revert.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visual Studio automatically formatting it.
Could you tell me how to revert?

{
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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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));
}
Expand Down
45 changes: 36 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this should be part of this commit? Seems like unrelated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IEngineFacade only have QueryAsync method, so GeocodingResponse geocode = geocodingEngine.Query(geocodeRequest); should be changed to var geocode = await geocodingEngine.QueryAsync(geocodeRequest);

var geocode = await geocodingEngine.QueryAsync(geocodeRequest);
Console.WriteLine(geocode);

// Static maps API - get static map of with the path of the directions request
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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<GoogleMapsApi.StaticMaps.Entities.Path>(){ new GoogleMapsApi.StaticMaps.Entities.Path()
{
Style = new PathStyle()
{
Color = "red"
},
EncodePolyline = overviewEncodePath
}}
,
IsSSL = true
,
ApiKey = googleAPIKey
});
Console.WriteLine("Map with EncodePath: " + encodePathUrl);
```