PARSEPATH

1701 단어
function parsePath(info){
let { path, zoom, offset } = info;

var steps = path.steps;
var lineArr = [];
steps.forEach(function(item, index){
    var tmparr = item.polyline.split(";");
    var tmp = tmparr.map(function(val){
        return val.split(",");
    });
    lineArr = lineArr.concat(tmp);
});

var pxarr = lineArr.map(function(item){
    return project({
        lng : item[0],
        lat : item[1]
    }, 20);
});

var pas = [];
var ratio = Math.pow(2, 20 - zoom);
pxarr.forEach(function(item, index){
    if(index % 4 === 0) {
        pas.push([((item.x - offset.x) /ratio).toFixed(6),((item.y - offset.y)/ratio).toFixed(6)]);
    }
});

return pas;

}
function pathLength(pas) {
var len = 0;
var i = 1;
if(pas && pas.length) {
    for( i; i

}
function fetchPath(opt){
var from = opt.origin.x + "," + opt.origin.y;
var to = opt.dest.x + "," + opt.dest.y
var url = "http://restapi.amap.com/v3/direction/driving?key=ac92f92386cfe54122ae34476aafb03e&extensions=base&strategy=" + parseInt(1 + 20 * Math.random()) + "&origin=" + from + "&destination=" + to;
fetch(url)
.then( (res) => {
    return res.json();
})
.then( (json) => {
    return {
        path : json.route.paths[0],
        zoom : opt.zoom,
        offset : opt.offset
    };
})
.then( parsePath )
.then( (pas) => {

    var len = pathLength(pas);

    postMessage({
        path : pas,
        len : len,
        opt : opt
    });
});

}

좋은 웹페이지 즐겨찾기