1

ポリライン スナップを道路に挿入しました。それは正常に動作します。ここで、別の分離されたポリライン スナップを道路に挿入して、同じマップに挿入したいと思います。そして、それはうまくいきません。最初のポリラインの終点を 2 番目のポリラインの始点に体系的に結合します。

ご協力いただきありがとうございます。

これが私のコードです

function initialize() {

var pos = new google.maps.LatLng(-26.431228,-69.572755);

var myOptions = {
    zoom: 5,
    center: pos,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById('map'), myOptions);

map.setCenter(pos);


//FIRST POLYLINE SNAP TO ROAD

ChileTrip1 = [
    new google.maps.LatLng(-33.417723,-70.605018),
    new google.maps.LatLng(-33.022446,-71.551688)
    ];

var traceChileTrip1 = new google.maps.Polyline({
    path: ChileTrip1,
    strokeColor: "red",
    strokeOpacity: 1.0,
    strokeWeight: 2
});

var service1 = new google.maps.DirectionsService(),traceChileTrip1,snap_path=[];
traceChileTrip1.setMap(map);
for(j=0;j<ChileTrip1.length-1;j++){
    service1.route({origin: ChileTrip1[j],destination: ChileTrip1[j+1],travelMode: google.maps.DirectionsTravelMode.DRIVING},function(result, status) {
        if(status == google.maps.DirectionsStatus.OK) {
            snap_path = snap_path.concat(result.routes[0].overview_path);
            traceChileTrip1.setPath(snap_path);
        }
    });
}

//SECOND POLYLINE SNAP TO ROAD

ChileTrip2 = [
    new google.maps.LatLng(-29.959694,-71.30825),
    new google.maps.LatLng(-32.778038,-71.181908)
];

var traceChileTrip2 = new google.maps.Polyline({
    path: ChileTrip2,
    strokeColor: "blue",
    strokeOpacity: 1.0,
    strokeWeight: 2
});

var service2 = new google.maps.DirectionsService(),traceChileTrip2,snap_path=[];
traceChileTrip2.setMap(map);
for(j=0;j<ChileTrip2.length-1;j++){
    service2.route({origin: ChileTrip2[j],destination: ChileTrip2[j+1],travelMode: google.maps.DirectionsTravelMode.DRIVING},function(result, status) {
        if(status == google.maps.DirectionsStatus.OK) {
            snap_path = snap_path.concat(result.routes[0].overview_path);
            traceChileTrip2.setPath(snap_path);
        }
    });
}

    };
    window.onload = function() { initialize();};
4

1 に答える 1

4

DirectionsService は非同期です。使用する前にコールバック ルーチン内の snap_path 配列をクリアするか、2 つの個別の snap_path 配列を作成します。

function initialize() {

var pos = new google.maps.LatLng(-26.431228,-69.572755);

var myOptions = {
    zoom: 5,
    center: pos,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById('map'), myOptions);

map.setCenter(pos);


//FIRST POLYLINE SNAP TO ROAD

ChileTrip1 = [
    new google.maps.LatLng(-33.417723,-70.605018),
    new google.maps.LatLng(-33.022446,-71.551688)
    ];

var traceChileTrip1 = new google.maps.Polyline({
    path: ChileTrip1,
    strokeColor: "red",
    strokeOpacity: 1.0,
    strokeWeight: 2
});

var service1 = new google.maps.DirectionsService(),traceChileTrip1,snap_path1=[];
traceChileTrip1.setMap(map);
for(j=0;j<ChileTrip1.length-1;j++){
    service1.route({origin: ChileTrip1[j],destination: ChileTrip1[j+1],travelMode: google.maps.DirectionsTravelMode.DRIVING},function(result, status) {
        if(status == google.maps.DirectionsStatus.OK) {
            snap_path1 = snap_path1.concat(result.routes[0].overview_path);
            traceChileTrip1.setPath(snap_path1);
        } else alert("Directions request failed: "+status);        
    });
}

//SECOND POLYLINE SNAP TO ROAD

ChileTrip2 = [
    new google.maps.LatLng(-29.959694,-71.30825),
    new google.maps.LatLng(-32.778038,-71.181908)
];

var traceChileTrip2 = new google.maps.Polyline({
    path: ChileTrip2,
    strokeColor: "blue",
    strokeOpacity: 1.0,
    strokeWeight: 2
});

var service2 = new google.maps.DirectionsService(),traceChileTrip2,snap_path2=[];
traceChileTrip2.setMap(map);
for(j=0;j<ChileTrip2.length-1;j++){
    service2.route({origin: ChileTrip2[j],destination: ChileTrip2[j+1],travelMode: google.maps.DirectionsTravelMode.DRIVING},function(result, status) {
        if(status == google.maps.DirectionsStatus.OK) {
            snap_path2 = snap_path2.concat(result.routes[0].overview_path);
            traceChileTrip2.setPath(snap_path2);
        } else alert("Directions request failed: "+status);        
    });
}

    };
    window.onload = function() { initialize();};

実施例

overview_path は「簡略化」されており、必ずしも道をたどるとは限らないことに注意してください。正確なルートが必要な場合は、すべての区間を処理する必要があります。

于 2013-08-01T12:53:20.243 に答える