javascriptmysqlgoogle-mapsgoogle-maps-api-3

Saving and recreating custom routes with waypoints in Google maps API


How do I save the custom route with waypoints and then re-create it back?

I created a map where user can plot his routes with start point, end point and 8 defaults waypoints in between.

(won't bother with the code- it is pretty generic)

My current issue is that I can't figure out how to save this custom route to the database and re-create it back when needed? Route can be separated into chunks and those chunks can be saved; but it is too many of them and I don't even know how would google handle it.

Wondering if there is any way to achieve my goal.


Solution

  • One way that worked good for me on a previous project is to make use of polyLine encoding and decoding. The advantage here is, that you generate a string that contains the entire polyLine with all it´s Waypoints.

    The Documentation + example can be found here

    The use of it is relativ easy. You need the PATH of the polyline you want to save:

    var encodedString = gmaps.geometry.encoding.encodePath(path);
    

    Now you would send this string to the database (keep track of the column length).

    The process of restoring is as easy as the encoding process. Make an request to your database to get your encoded polyLine string and do:

    request.get(dataToSend, function (r) {
            if (r !== null) {
                var decodedString = gmaps.geometry.encoding.decodePath(r[0]'pos']);
                restoredPolyLine = new gmaps.Polyline({
                    path: decodedString,
                    strokeColor: LINE_COLOR,
                    strokeOpacity: 1.0,
                    strokeWeight: 3,
                    editable: false,
                    map: map
                });
            }
        });
    

    Hope I have understand your Question correct and my answer could help you. Please let me know.