高德地图货车驾车轨迹,webapi 快速批量获取2点之间的轨迹数据







var data = {
    "state": true,
    "info": "成功",
    "data": [
       {"coordinate":[116.714919,23.396726],"route_sequence":18,"section_sequence":18},{"coordinate":[116.71399,23.398669],"route_sequence":19,"section_sequence":19},{"coordinate":[116.71101,23.393503],"route_sequence":20,"section_sequence":20},{"coordinate":[116.741079,23.395443],"route_sequence":22,"section_sequence":22},{"coordinate":[116.716585,23.393869],"route_sequence":23,"section_sequence":23},{"coordinate":[116.770493,23.398498],"route_sequence":26,"section_sequence":26},{"coordinate":[116.767268,23.401064],"route_sequence":31,"section_sequence":31},{"coordinate":[116.773984,23.395659],"route_sequence":33,"section_sequence":33},{"coordinate":[116.767733,23.410942],"route_sequence":34,"section_sequence":34},{"coordinate":[116.670037,23.353066],"route_sequence":79,"section_sequence":34},{"coordinate":[116.671359,23.352991],"route_sequence":80,"section_sequence":35},{"coordinate":[116.677534,23.355971],"route_sequence":81,"section_sequence":36},{"coordinate":[116.669183,23.35402],"route_sequence":82,"section_sequence":37},{"coordinate":[116.671359,23.352991],"route_sequence":83,"section_sequence":38}

    ],
    "coordinate": [116.783822, 23.42112],
    "type": "section"
};



var gd={
    kk:0,
    keys:[
        'key1','key2','key3'
    ],

    arrayData: function (data) {
        var arrs0=gd.arrData(data);
        console.log(arrs0.length);
        var arrs=[];
        var ks=gd.keys.length;
        var as=Math.ceil(arrs0.length/ks);
        for(var i=0;i<as;i++){
            if(arrs0.length>ks){
                var a=arrs0.slice(0,ks);
                arrs0=arrs0.splice(ks,arrs0.length);
                arrs.push(a);
            }else{
                var a=arrs0.slice(0,arrs0.length);
                arrs.push(a);
            }
        }
        console.log(arrs.length);
        return arrs;
    }
    ,

    get:function (getStr,callback){
        try{
            http.get(getStr, function(req,res) {
                var jsonStr='';
                req.on('data',function(data){
                    jsonStr+=data;
                });
                req.on('end',function(){
                    var obj=JSON.parse(jsonStr);
                    if(obj.errcode){
                        callback('err',obj);
                        return false;
                    }else{
                        if(obj.data.route==null){
                            callback('err',obj);
                        }else{
                            var arrs=gd.gdrouteData(obj);
                            callback(arrs,obj);
                        }
                    }
                });
            }).on('error', function(e) {
                console.log(e); return false;
                callback('err',{});
            });
        }catch(e){console.log(e); return false;}
    },


    arrData: function (data) {
        var obj=data.data;
        obj.sort(function (x,y) {
            return x.route_sequence-y.route_sequence;
        });
        var arrs0=[];
        var k=1;
        arrs0.push({c:data.coordinate,s:k});
        for(var i=0;ivar s=arrs0.length;
        arrs0.push({c:data.coordinate,s:s});
        var array2=[];
        for(var k=0;kif(arrs0[k+1]){
                array2.push({s:k+1,c1:arrs0[k].c,c2:arrs0[k+1].c});
                //console.log('----------------------',k,array2[k]);
            }
        }
        return array2;
    },

    gdrouteData_Old:function (obj){
        var arrs=[];
        if(!obj.data.route.paths){
            cosnole.log('obj.data.route.paths不存在',obj.data.route);
            return false;
        }
        var odrp=obj.data.route.paths;
        for(var i=0;ivar p=odrp[i].steps;
            for(var j=0;j< p.length;j++){
                var str=p[j].polyline+';';
                var  strss=str.split(';');
                for(var k=0;kif(strss[k]!=''){
                        arrs.push(strss[k].split(','));
                    }
                }
            }
        }
        return   arrs;
    }
    ,
    gdrouteData:function (obj){
        var arrs=[];
        if(!obj.data.route.paths){
            cosnole.log('obj.data.route.paths不存在',obj.data.route);
            return false;
        }
        var arr=[];
        var odrp=obj.data.route.paths;
        for(var i=0;ifunction (x,y) {return x.distance-y.distance;});
        var p=arr[0].paths.steps;
        for(var j=0;j< p.length;j++){
            var str=p[j].polyline+';';
            var  strss=str.split(';');
            for(var k=0;kif(strss[k]!=''){
                    arrs.push(strss[k].split(','));
                }
            }
        }
        return   arrs;
    }

    ,
    getStr: function (arrs0,key) {
        return 'http://restapi.amap.com/v4/direction/truck?'+qs.stringify({width:2.5, strategy:5, size:2, weight:10, axis:2, height:1.6, load:0.9, key:key,
                origin:arrs0.c1[0]+','+arrs0.c1[1],//起点
                destination:arrs0.c2[0]+','+arrs0.c2[1]//终点
            });
    },

    gdMapGet:function (arrs0,callback){
        if(arrs0){
            if(gd.kk==gd.keys.length){gd.kk=0;}
            gd.get(gd.getStr(arrs0,gd.keys[gd.kk]), function (data,obj) {
                (data=='err')?gd.gdMapGet2(arrs0,callback):callback(1,arrs0.s,data,obj);
            });
            gd.kk++;
        }else{
            console.log('==============不存在===============')
        }
    },
    gdMapGet2:function (arrs0,callback){
        if(arrs0){
            if(gd.kk==gd.keys.length){gd.kk=0;}
            gd.get(gd.getStr(arrs0,gd.keys[gd.kk]), function (data,obj) {
                (data=='err')?gd.gdMapGet2(arrs0,callback):callback(2,arrs0.s,data,obj);
            });
            gd.kk++;
        }else{
            console.log('==============不存在===============')
        }
    },
    main: function (i,arrs0,callback) {
        if(!arrs0[i]){
            console.log('结束触发路线查询');
            return false;
        }
        console.log('触发查询,i=',i);
        for(var key=0;keyfunction () {
            gd.main(i,arrs0,callback);
        },100);
    }
};



你可能感兴趣的:(原创)