从网络服务我收到随机选择的坐标列表。当我在诺基亚这里绘制它们并试图找出它们中最短的路线时,它导致了一种分散的方式。
进一步向下钻取显示我没有对那些坐标进行排序。
有没有办法对坐标进行排序,同时将它们转换为路径列表,或者我必须在绘制它们之前对所有坐标进行排序?提前致谢
代码如下:
router = new nokia.maps.routing.Manager();
var onRouteCalculated = function (observedRouter, key, value) {
if (value == "finished") {
var routes = observedRouter.getRoutes();
//create the default map representation of a route
mapRoute = new nokia.maps.routing.component.RouteResultSet(routes[0]).container;
map.objects.add(mapRoute);
//Zoom to the bounding box of the route
map.zoomTo(mapRoute.getBoundingBox(), false, "default");
} else if (value == "failed") {
alert("The routing request failed.");
}
};
router.addObserver("state", onRouteCalculated);
var modes = [{
type: "shortest",
transportModes: ["car"],
options: "avoidTollroad",
trafficMode: "default"
}];
function CreateRouteByMultipleWaypoints() {
if (typeof (mapRoute) != 'undefined' || mapRoute != null) {
map.objects.remove(mapRoute);
}
router.clear();
var waypoints = new nokia.maps.routing.WaypointParameterList();
var RUidsByChoice = $('#dialogforPlotRoute').text().trim().split(',');
for (index = 0; index < selectedcoOrdinatestoPlanRoute.length; index++) {
waypoints.addCoordinate(new nokia.maps.geo.Coordinate(parseFloat(selectedcoOrdinatestoPlanRoute[index].split('>')[0]), parseFloat(selectedcoOrdinatestoPlanRoute[index].split('>')[1])));
}
router.calculateRoute(waypoints, modes);
}
答案 0 :(得分:0)
我使用旅行商问题算法找出所有给定坐标中的最短路径
function sortWayPoints() {
var coordinatsArraytoPlot = selectedcoOrdinatestoPlanRoute;
//Starting location some hard coded value
var startPoint = new nokia.maps.geo.Coordinate(39.7391500, -104.9847000);
var mainArray = [];
mainArray.push(startPoint);
while (coordinatsArraytoPlot.length > 0) {
var sortArr = [];
for (index = 0; index < coordinatsArraytoPlot.length; index++) {
var coordinate2 = new nokia.maps.geo.Coordinate(parseFloat(coordinatsArraytoPlot[index].split('>')[0]), parseFloat(coordinatsArraytoPlot[index].split('>')[1]));
var dist = startPoint.distance(coordinate2);
sortArr.push(dist);
}
var test = sortArr[0];
var lessiNDEX = 0;
for (var i = 1; i < sortArr.length; i++) {
if (sortArr[i] < test) {
test = sortArr[i];
lessiNDEX = i;
}
}
// The co ordinates are available in format "lat>long" so i have to
//split them and convert them to parseflot to get compatible result
startPoint = new nokia.maps.geo.Coordinate(parseFloat(coordinatsArraytoPlot[lessiNDEX].split('>')[0]), parseFloat(coordinatsArraytoPlot[lessiNDEX].split('>')[1]));
mainArray.push(startPoint);
//Remove calculated co ordinate from main array
coordinatsArraytoPlot.splice(lessiNDEX, 1);
}
//mainArray will contain all sorted Co ordinates
}