axel wolf
axel wolf

Reputation: 1478

HERE Maps Javascript API alternative routes

Is there a way to use nokia.maps.routing.Manager with alternative routes?

Because if I add "alternatives=2" to resulting request URL I get more than just 1 result.

original:

http://route.api.here.com/routing/7.2/calculateroute.json?routeattributes=shape&maneuverattributes=all&jsonAttributes=1&waypoint0=geo!52.51607,13.37698&waypoint1=geo!48.13641,11.57753&language=de-DE&mode=fastest;car;traffic:enabled;&app_id=id7LcG3L4lqQgdqrmwKI&app_code=9PdZ8eZhq0IwHaF7IT5oUA&xnlp=CL_JSMv2.5.3,SID_3023C830-AC2E-436B-8AC7-4DB1C679438D

in response json: response.route contains 1 element

modified (look at the very end - "&alternatives=2"):

http://route.api.here.com/routing/7.2/calculateroute.json?routeattributes=shape&maneuverattributes=all&jsonAttributes=1&waypoint0=geo!52.51607,13.37698&waypoint1=geo!48.13641,11.57753&language=de-DE&mode=fastest;car;traffic:enabled;&app_id=id7LcG3L4lqQgdqrmwKI&app_code=9PdZ8eZhq0IwHaF7IT5oUA&xnlp=CL_JSMv2.5.3,SID_3023C830-AC2E-436B-8AC7-4DB1C679438D&alternatives=2

in response json: response.route contains 3 elements

So there is a way. I just don't know how to get there using the nokia.maps.routing.Manager

Upvotes: 0

Views: 751

Answers (1)

DomSchuette
DomSchuette

Reputation: 101

The nokia.maps.routing.Manager (based on 7.2 Routing) doesn't support alternatives as Parameter. To pass this Parameter to the Routing, you need to use a JSONP call to the Backend Routing Server:

startRouting = function()
{
    var routeUrl = ["http://route.nlp.nokia.com/routing/7.2/calculateroute.json?",
                  "app_id=" + app_id + "&",
                  "app_code=" + app_code + "&",
                  "waypoint0=" + startLat + ","+ startLng + "&",
                  "waypoint1="+ destLat + "," + destLng + "&", 
                  "instructionformat=text" + "&",
                  "jsonAttributes=33" + "&",
                  "mode=fastest;car;traffic:disabled&",
                  "routeattributes=sh",
                  "&alternatives=2",
                  "&jsoncallback=routingcallback"].join("");

    script = document.createElement("script");
    script.src = routeUrl;
    document.body.appendChild(script);
}

var strokeColor = ["#7a24db", "#85db24","#8f0404", "#fdf700"];

routingcallback = function(data)
{
    var routes = data.response.route;
    for(var i = 0; i < routes.length; i++)
    {
        var coords = routes[i].shape;

        var shape = nokia.maps.geo.Shape.fromLatLngArray(coords, false);

        var curColor = strokeColor[i]; 
        if(routes.length == 1)
            curColor = strokeColor[3];

        var line = new nokia.maps.map.Polyline(shape, { pen: { strokeColor: curColor, lineWidth: 4 } });

        display.objects.add(line);
        display.zoomTo(line.getBoundingBox(), false, "default");
    }
}

Upvotes: 2

Related Questions