诺基亚这里在构建具有多个坐标的路线时会出现错误

时间:2014-06-10 20:25:55

标签: javascript polyline here-api

例如,我有500个路线坐标,我想建立路线使用从A点到B点的坐标

现在我使用了这段代码:

router = new nokia.maps.routing.Manager();

    // The function onRouteCalculated  will be called when a route was calculated
        var onRouteCalculated = function (observedRouter, key, value) {
            if (value == "finished") {
                var routes = observedRouter.getRoutes(),
                    container = new nokia.maps.map.Container(),
                    route = routes[0],
                    waypoints = route.waypoints,
                    i, length = waypoints.length;

                // Add route polyline to the container
                container.objects.add(new nokia.maps.map.Polyline(route.shape, {
                    pen : new nokia.maps.util.Pen({
                        lineWidth: 5,
                        strokeColor: "#AB7A8C"
                    })
                }));

                // Add container to the map
                map.objects.add(container);

                // Iterate through all waypoints and add them to the container
                for (i = 0; i < length; i++) {
                    //
                    container.objects.add(new nokia.maps.map.StandardMarker(waypoints[i].originalPosition, {
                        text: String.fromCharCode(65 + i) //65 is a char code for "A"
                    }));
                }
                //Zoom to the bounding box of the route
                map.zoomTo(container.getBoundingBox(), false, "default");
            } else if (value == "failed") {
                alert("The routing request failed.");
            }
        };

        /* We create on observer on router's "state" property so the above created
         * onRouteCalculated we be called once the route is calculated
         */
        router.addObserver("state", onRouteCalculated);

    // Create waypoints
        var waypoints = new nokia.maps.routing.WaypointParameterList();

        $.each(routesArr, function (key, value){
nokia.maps.geo.Coordinate(parseFloat(value[1]), parseFloat(value[0])));
                var Coords = value.split(' ');
                console.log(Coords);
                waypoints.addCoordinate(new nokia.maps.geo.Coordinate(parseFloat(Coords[1]), parseFloat(Coords[0])));
        })

        console.log(waypoints);

        /* Properties such as type, transportModes, options, trafficMode can be
         * specified as second parameter in performing the routing request.
         *
         * See for the mode options the "nokia.maps.routing.Mode" section in the developer's guide
         */
        var modes = [{
            type: "fastest",
            transportModes: ["car"],
            trafficMode: "disabled",
            options: ""
        }];

    // Trigger route calculation after the map emmits the "displayready" event
        map.addListener("displayready", function () {
            router.calculateRoute(waypoints, modes);
        }, false);

但在debuger中有错误:

["32.0966977626384", "49.4231563713862"] functions.js:142
["32.0968187973202", "49.4231926231174"] functions.js:142
["32.0967591181696", "49.4231632026373"] functions.js:142
["32.0967998542191", "49.4231820200099"] functions.js:142
["32.0968651492448", "49.4232476922212"] functions.js:142
["32.0969191287012", "49.4233110594092"] functions.js:142
["32.0969724376054", "49.4233680982603"] functions.js:142
["32.0970814023466", "49.4234444154887"] functions.js:142
["32.0972258225382", "49.4235442020459"] functions.js:142
["32.0974480267913", "49.4237132650329"] functions.js:142
["32.0977787766904", "49.4239508081688"] functions.js:142
["32.0976077020467", "49.4238198828412"] functions.js:142
["32.0980493445248", "49.4241391076235"] functions.js:142
["32.0982283819766", "49.4242630759714"] functions.js:142
["32.0986557752193", "49.4245636510192"] functions.js:142
["32.0987762231679", "49.424653169745"] functions.js:142
["32.0984075032474", "49.4243849907531"] functions.js:142
["32.0988543425054", "49.4246631442098"] functions.js:142
["32.0988309569956", "49.4246686343564"] functions.js:142
["32.0988922287078", "49.4246461708559"] functions.js:142
["32.0989042148293", "49.4246334722726"] functions.js:142
["32.0989042148293", "49.4246334722726"] functions.js:142

 GET http://route.st.nlp.nokia.com/routing/6.2/calculateroute.json?routeattribut…ck=(function()%7Bnokia.maps.net.Request.callbacks%5B4%5D(5,arguments);%7D) 502 (BAD_GATEWAY) base.js:167
    Uncaught TypeError: Cannot read property 'error' of undefined 

我无法解决这个问题可以有人碰到这个吗?

1 个答案:

答案 0 :(得分:1)

如果您只是在地图上画线,不想要​​Route,则需要Polyline

e.g。

// Set of initial geo coordinates to create the purple polyline
var points = [
  new nokia.maps.geo.Coordinate(50.1299, 8.5680),
  new nokia.maps.geo.Coordinate(50.0969, 8.4829),
  new nokia.maps.geo.Coordinate(50.0819, 8.4856),
  new nokia.maps.geo.Coordinate(50.0555, 8.4479)
];

// Transparent purple polyline
var polyline = new nokia.maps.map.Polyline(
  points,
  { 
    pen: {
      strokeColor: "#22CA", 
      lineWidth: 5
    }
  }
);

 map.objects.add(polyline);

目前您的样本正在尝试计算驾驶指令从A到B到C到D ....到第500点,我怀疑这是你想要的。您可以定义的中间点数量有限,如果我记得的话,大约是10个。

您可以使用以下方式zoomTo路线:

map.zoomTo(polyline.getBoundingBox(), false, "default");