2013-08-01 2 views
1

도로에 폴리 라인 스냅을 삽입했습니다. 그것은 잘 작동합니다. 이제 동일한 맵에서 도로에 다른 분리 된 폴리 라인 스냅을 삽입하고 싶습니다. 그리고 그것은 잘 작동하지 않습니다. 첫 번째 폴리 라인의 끝점과 두 번째 폴리 라인의 시작점을 체계적으로 조인합니다.Google지도 API - 도로 폴리선에 여러 스냅

도움 주셔서 감사합니다.

여기하려면 DirectionsService가 비동기 내 코드

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

입니다. 어느 쪽을 사용하기 전에 콜백 루틴 내부의 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();}; 

working example

주를 overview_path - 길가 "단순화"이며, 반드시 도로를 따라하지 않습니다. 정확한 경로가 필요한 경우 모든 다리를 처리해야합니다.

+0

overview_path로 여러 번 시도해보고 도로를 따라갈 때마다 시도했습니다. 그래서 반드시 길을 따라 가지 않을 것이라고 말하는 것은 무엇을 의미합니까? 또한 모든 다리를 처리하는 것의 의미는 무엇입니까? 도로 구간별로 폴리 라인을 의미합니까? – user2543010

+0

Coquimbo 근처의 파란 선을 자세히보십시오. [다리를 파싱하는 예제] (http://www.geocodezip.com/v3_directions_custom_iconsC.html) – geocodezip

+0

사실, 폴리 라인이 때로는 길 밖으로 나가는 것을 볼 수 있습니다. 이제, 다리를 파싱한다는 것은 좌표 대신 시작 및 끝 주소를 삽입한다는 것을 말할 수 있습니까? – user2543010