|
1 |
| -# polyline-algorithm-csharp |
| 1 | +# .NET Polyline Algorithm (.NET Standard 2.0) |
| 2 | + |
| 3 | +Lightweight .NET Standard 2.0 library implementing <a href="https://developers.google.com/maps/documentation/utilities/polylinealgorithm">Google Polyline Algorithm</a>. Designed with respect to flexibility, but still with easy to use. |
| 4 | + |
| 5 | +## Getting started |
| 6 | +### Prerequisites |
| 7 | + |
| 8 | +.NET Polyline Algorithm is avalable as nuget package <a href="https://www.nuget.org/packages/Cloudikka.PolylineAlgorithm/">Cloudikka.PolylineAlgorithm</a> targeting .NET Standard 2.0. |
| 9 | + |
| 10 | +Command line: |
| 11 | + |
| 12 | +`Install-Package Cloudikka.PolylineAlgorithm` |
| 13 | + |
| 14 | +NuGet Package Manager: |
| 15 | + |
| 16 | +`Cloudikka.PolylineAlgorithm` |
| 17 | + |
| 18 | +#### Warning |
| 19 | + |
| 20 | +Library is using <a href="https://msdn.microsoft.com/en-us/library/mt744804(v=vs.110).aspx">ValueTuple<T1, T2> Structure</a>. ValueTuple struct is avalable in .NET Framework 7.0 and above. Incase your project is targeting lower version of .NET Framework you probably have to install <a href="https://www.nuget.org/packages/System.ValueTuple/">System.ValueTuple</a> NuGet package. (not tested yet) |
| 21 | + |
| 22 | +Command line: |
| 23 | + |
| 24 | +`Install-Package System.ValueTuple` |
| 25 | + |
| 26 | +NuGet Package Manager: |
| 27 | + |
| 28 | +`System.ValueTuple` |
| 29 | + |
| 30 | +### Hot to use it |
| 31 | + |
| 32 | +There are three ways how to use .NET Polyline Algorithm library based on your needs. For each is available Encode and Decode methods. |
| 33 | + |
| 34 | +#### Static methods |
| 35 | + |
| 36 | +Whenever you just need to encode or decode Google polyline you can use static methods defined in static PolylineAlgorithm class. |
| 37 | + |
| 38 | +##### Decoding |
| 39 | + |
| 40 | +```csharp |
| 41 | + string polyline = "polyline"; |
| 42 | + IEnumerable<(double, double)> coordinates = PolylineAlgorithm.Decode(polyline); |
| 43 | +``` |
| 44 | + |
| 45 | +##### Encoding |
| 46 | + |
| 47 | +```csharp |
| 48 | + IEnumerable<(double, double)> coordinates = new (double, double) [] { (35.635, 76.27182), (35.2435, 75.625), ... }; |
| 49 | + string polyline = PolylineAlgorithm.Encode(coordinates); |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +#### Default instance |
| 54 | + |
| 55 | +If you need to use dependency injection, you would like to have instance to deliver the work for you. In that case you can use default instance of PolylineEncoding class, which implements IPolylineEncoding<(double Latitude, double Longitude)> interface. |
| 56 | + |
| 57 | +##### Decoding |
| 58 | + |
| 59 | +```csharp |
| 60 | + string polyline = "polyline"; |
| 61 | + var encoding = new PolylineEncoding(); |
| 62 | + IEnumerable<(double, double)> coordinates = encoding.Decode(polyline); |
| 63 | +``` |
| 64 | + |
| 65 | +##### Encoding |
| 66 | + |
| 67 | +```csharp |
| 68 | + IEnumerable<(double, double)> coordinates = new (double, double) [] { (35.635, 76.27182), (35.2435, 75.625), ... }; |
| 69 | + var encoding = new PolylineEncoding(); |
| 70 | + string polyline = encoding.Encode(coordinates); |
| 71 | +``` |
| 72 | + |
| 73 | +#### Inherited base class |
| 74 | + |
| 75 | +There may be a scenario you need to pass and return different types to and from without a need to add another extra layer. In this case you can inherit PolylineEncodingBase<T> class and override template methods CreateResult and GetCoordinates. |
| 76 | + |
| 77 | +##### Inheriting |
| 78 | + |
| 79 | +```csharp |
| 80 | + public class MyPolylineEncoding : PolylineEncodingBase<Coordinate> { |
| 81 | + |
| 82 | + protected override Coordinate CreateResult(double latitude, double longitude) { |
| 83 | + return new Coordinate(latitude, longitude); |
| 84 | + } |
| 85 | + |
| 86 | + protected override (double Latitude, double Longitude) GetCoordinate(Coordinate source) { |
| 87 | + return (source.Latitude, source.Longitude); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | +``` |
| 92 | + |
| 93 | +##### Decoding |
| 94 | + |
| 95 | +```csharp |
| 96 | + string polyline = "polyline"; |
| 97 | + var encoding = new MyPolylineEncoding(); |
| 98 | + IEnumerable<Coordinate> coordinates = encoding.Decode(polyline); |
| 99 | +``` |
| 100 | + |
| 101 | +##### Encoding |
| 102 | + |
| 103 | +```csharp |
| 104 | + IEnumerable<Coordinate> coordinates = new Coordinate [] { new Coordinate(35.635, 76.27182), new Coordinate(35.2435, 75.625), ... }; |
| 105 | + var encoding = new MyPolylineEncoding(); |
| 106 | + string polyline = encoding.Encode(coordinates); |
| 107 | +``` |
0 commit comments