C-Style C++ implementation of Google Encoded Polyline Algorithm Format forked from PolylineEncoder
No installation required. Just compile polylineencoder.h(.cpp) in your project and use Polyline
namespaced functions and the Step_decoder
Class.
No special requirements except a C++ compliant compiler. The class is tested with g++ 7.4.0.
This library is designed to run on embedded systems and does not use resource intensive C++ functionality or the Standard Library. The class encapsulation is useful if the state of several StepEncoders
should be preserved. However, it would be trivial to port it to pure C.
The library can be used to en/decode polylines one point or character at a time. This allows stream-like implementation for on-the-fly encoding / decoding and minimal memory consumption.
char line[50]; // Big enough for result.
Polyline::Point points[3]; // Array of points forms a line.
points[0] = {38.5, -120.2}; // Add points.
points[1] = {40.7, -120.95};
points[2] = {43.252, -126.453};
size_t ret; // Save return value for overrun error checking.
ret = Polyline::encode_line(points, 3, line, sizeof(line)); // encode
if( ret != 3 ) { ... } // check for sucessful encoding
// line now contains "_cidP_gsia@~bidP~fsia@~bidP~fsia@" (null terminated)
ret = Polyline::decode_line(line, points, 3) //decode
if( ret != 3 ) { ... } // check for sucessful decoding
// points contains original coordinates again.
For an example of the Step_decoder
class, look at the decode_line()
function which uses the class internally. Similarly, the encode_line()
function can be used as a reference for using the encode_point()
function.
There are unit tests provided in the test/ directory. To run them you have to build and run the test application. For doing that you must invoke the following commands from the terminal, assuming that compiler and environment are already configured:
cd test
g++ -std=c++11 test.cpp -o test.out
./test.out