2013-08-03 12 views
5

現在、Google Maps API V3で直線ルートを取得する方法を探しています。GoogleマップApiストレート(最短)ルート

ジオコードと方向サービスを使用して、2つの代替ルートを含むポイントAからポイントBまでのルートを取得できました。私はまた、 "高速道路なし"と "通行料なし"で実験しましたが、この問題を完全に解決するものは何もありません... 現時点で私は3つの特定のルートをチェックしていますが、これは最短ルート。

私は、最速または最短ルートが、として低マイルできるだけとちょうどストレート経路を探索ないです。

スレッドをGoogleを使用して説明すると、私はあなたに尋ねる必要があります。おそらく誰かが解決策を持っているかもしれません...

P .:私たちの搭載されたナビゲーションシステムが再び機能しないとき、私たちの地元の消防車のナビゲーションヘルプとして使用されるので、私も使用できません。 これも私たちができるだけ低いキロメートルを必要とする理由です - 最も速いルートがここで最も速いルートが99%最も小さいマイルであるが、Apiはそれを決めることはできませんし、主要道路の使用を主張しません

+0

あなたは今まで解決策を見つけましたか? – Tarlen

+0

Soldeplata Saketosの回答は現時点では最良の解決策であるようです。私が期待したものではありませんが、私たちが現在入手できる最も近いものだと思います。 – user2649424

答えて

1

AからBIへの最短ルートを取得するには、 "alternatives = true"パラメータで異なるクエリを作成し、avoid = toll、avoid = highwaysの間の "avoid"パラメータで再生し、最短ルートを選んでください。

directionsService = new google.maps.DirectionsService; 
//avoiding tolls 
      directionsService.route({ 
       origin: { 
        'placeId': originId 
       }, 
       destination: { 
        'placeId': destinationId 
       }, 
       provideRouteAlternatives: true, 
       avoidTolls: true, 
       travelMode: google.maps.TravelMode.DRIVING 
      }, function(response, status) { 
       if (status === google.maps.DirectionsStatus.OK) { 
        routesResponses.push(response); 
       } 
       else { 
        window.alert('Directions request failed due to ' + status); 
       } 
      }); 
      //avoiding highways 
      directionsService.route({ 
       origin: { 
        'placeId': originId 
       }, 
       destination: { 
        'placeId': destinationId 
       }, 
       provideRouteAlternatives: true, 
       avoidHighways: true, 
       travelMode: google.maps.TravelMode.DRIVING 
      }, function(response, status) { 
       if (status === google.maps.DirectionsStatus.OK) { 
        routesResponses.push(response); 
       } 
       else { 
        window.alert('Directions request failed due to ' + status); 
       } 

       //Results analysis and drawing of routes 
       var fastest = Number.MAX_VALUE, 
        shortest = Number.MAX_VALUE; 

       routesResponses.forEach(function(res) { 
        res.routes.forEach(function(rou, index) { 
         console.log("distance of route " +index+": " , rou.legs[0].distance.value); 
         console.log("duration of route " +index+": " , rou.legs[0].duration.value); 
         if (rou.legs[0].distance.value < shortest) shortest = rou.legs[0].distance.value ; 
         if (rou.legs[0].duration.value < fastest) fastest = rou.legs[0].duration.value ; 

        }) 
       }) 
       console.log("shortest: ", shortest); 
       console.log("fastest: ", fastest); 
//painting the routes in green blue and red 
       routesResponses.forEach(function(res) { 
        res.routes.forEach(function(rou, index) { 
         new google.maps.DirectionsRenderer({ 
          map:map, 
          directions:res, 
          routeIndex:index, 
          polylineOptions:{ 
           strokeColor: rou.legs[0].duration.value == fastest? "red":rou.legs[0].distance.value == shortest?"darkgreen":"blue", 
           strokeOpacity: rou.legs[0].duration.value == fastest? 0.8:rou.legs[0].distance.value == shortest? 0.9: 0.5, 
           strokeWeight: rou.legs[0].duration.value == fastest? 9:rou.legs[0].distance.value == shortest? 8: 3, 
          } 
         }) 
        }) 
       }) 
      }); 
     } 

    } 
1

私は一度ルートサービスを呼び出して、あなたが便利を見つける方法で結果をフィルタリングすること、here言ったように私は、ソリューションを使用しています。私の例では、私は最短ルートを計算しています。

関連する問題