web-dev-qa-db-ja.com

Google Directions APIのリクエストあたり23ウェイポイントを超えています(ビジネス/ワークレベル)

Googleのルート案内APIを使用して、冬に除雪車を、夏に造園を行う会社のルート計画ソフトウェアを開発したいと思います。顧客の要件の1つは、少なくとも30(できればそれ以上)のウェイポイントでルートを計算できることです。ドキュメント(下記に引用)によると、Google Maps API for Workのお客様でさえ、リクエストごとに23ウェイポイントに制限されています。

Google Directions APIの使用には、1日あたり2,500件のルートリクエストのクエリ制限が適用されます。個々のルートリクエストには、リクエストに最大8つの中間ウェイポイントを含めることができます。 Google Maps API for Workのお客様は、1日あたり最大100,000件のルートリクエストをクエリできます。各リクエストで許可されるウェイポイントは最大23個です。

これを回避するための回避策を知っている人はいますか?

また、無料のAPIの回避策を使用することは可能でしょうか?最高のアカウントは非常に高価だと聞いています。

ありがとう!!マーク

27
Marc H

前回の電話でGoogle担当者と話をしたとき、最高価格は10,000ドルからかなり高くなっています。

8つのウェイポイントの制限を回避する方法で回避策を見つけました。私はそれを機能させることができました。

これを行うには、ウェイポイントを受け取り、それらを異なるルートに分割しますが、それらを同じルートとしてまとめます。

たとえば、30個のウェイポイントが必要な場合、4本の線を描画しますが、同じ色などを使用します。したがって、基本的にはウェイポイントを異なるルートにカットし、毎回異なるルートであるかのようにルートレンダラーを呼び出します。キーは最初のルートの後にあり、次のルートは前のルートの最後のウェイポイントから開始する必要があります(これにより、ルートラインが相互に接続されていることが確認されます)

動作しますが、最高のアカウントを持っている場合よりもはるかに多くのコードを記述する必要があり、このインスタンスではより多くの指示を求めています。

私は検索し、最高のアカウントを持たずにこれを行う他の方法について考えましたが、失敗しました。

しかし、私はグーグルと話すとき、彼らは異なる欲求/ニーズを持つ顧客のために支払いの階層構造を作成することを意図したと言っていました。たとえば、顧客がもっと多くのウェイポイントを必要とするだけで、より多くの方向要求が必要な場合。

練習アプリケーションで私のために働いたので、これが役立つことを願っています。

19
function initMap() {
    var service = new google.maps.DirectionsService;
    var map = new google.maps.Map(document.getElementById('map'));

    // list of points
    var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
    ];
    
    // Zoom and center map automatically by stations (each station will be in visible map area)
    var lngs = stations.map(function(station) { return station.lng; });
    var lats = stations.map(function(station) { return station.lat; });
    map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
    });
    
    // Show stations on the map as markers
    for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
            continue;
        new google.maps.Marker({
            position: stations[i],
            map: map,
            title: stations[i].name
        });
    }

    // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 Origin + 1 destination)
    for (var i = 0, parts = [], max = 8 - 1; i < stations.length; i = i + max)
        parts.Push(stations.slice(i, i + max + 1));

    // Callback function to process service results
    var service_callback = function(response, status) {
        if (status != 'OK') {
            console.log('Directions request failed due to ' + status);
            return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
    };
        
    // Send requests to service to get route (for stations count <= 25 only one request will be sent)
    for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (Origin) and last station (destination)
        var waypoints = [];
        for (var j = 1; j < parts[i].length - 1; j++)
            waypoints.Push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
            Origin: parts[i][0],
            destination: parts[i][parts[i].length - 1],
            waypoints: waypoints,
            travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
    }
  }
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}
#map {
    height: 100%;     
    width: 100%;
    height: 100%;
}
<div id="map"></div>

<!-- without API KEY set variable "max" to 8 -->
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

<!-- with API KEY set variable "max" to 25 -->
<!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->

次のコードを使用すると、必要な数のウェイポイントを使用でき、エラーMAX_WAYPOINTS_EXCEEDEDが発生することはありません。 「YOUR_API_KEY」をAPI KEYに置き換えるか、Google API URLから&key = YOUR_API_KEYを削除し、変数「max」を8(max = API KEYを使用する場合は25、API KEYを使用しない場合は最大= 8)。

<style>
html, body { height: 100%; margin: 0; padding: 0; }
#map { height: 100%; width: 100%; height: 100%; }
</style>
<div id="map"></div>
<script>
  function initMap() {
    var service = new google.maps.DirectionsService;
    var map = new google.maps.Map(document.getElementById('map'));

    // list of points
    var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        // ... as many other stations as you need
    ];

    // Zoom and center map automatically by stations (each station will be in visible map area)
    var lngs = stations.map(function(station) { return station.lng; });
    var lats = stations.map(function(station) { return station.lat; });
    map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
    });

    // Show stations on the map as markers
    for (var i = 0; i < stations.length; i++) {
        new google.maps.Marker({
            position: stations[i],
            map: map,
            title: stations[i].name
        });
    }

    // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 Origin + 1 destination)
    for (var i = 0, parts = [], max = 25 - 1; i < stations.length; i = i + max)
        parts.Push(stations.slice(i, i + max + 1));

    // Service callback to process service results
    var service_callback = function(response, status) {
        if (status != 'OK') {
            console.log('Directions request failed due to ' + status);
            return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
    };

    // Send requests to service to get route (for stations count <= 25 only one request will be sent)
    for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (Origin) and last station (destination)
        var waypoints = [];
        for (var j = 1; j < parts[i].length - 1; j++)
            waypoints.Push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
            Origin: parts[i][0],
            destination: parts[i][parts[i].length - 1],
            waypoints: waypoints,
            travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
    }
  }
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>

You can see screen here.

行ボタンの表示/非表示の調整

15
mikep

8つ以上のウェイポイントを使用するハックがあります。そのためのソリューションをチェックしてください。

Google Directions APIを使用して8つ以上のウェイポイントを含むロードマップを描く

0
Nullify

このソリューションには簡単な方法があります。

距離のしきい値に基づいてウェイポイントを配列に保持し、追加し続けます。配列内の8つの値の制限に達したら、ウェイポイント配列の最初の位置(オリジン)を新しいウェイポイント配列に割り当てます....最後のウェイポイントを2番目の要素として新しいウェイポイントに割り当てます...この新しいウェイポイント配列と古いウェイポイント配列を続けてください。

ウェイポイントを実行すると、値が8を超えることはありません。また、マップに表示されるルートを追跡します(旅が長すぎる場合を除きます)

var addWaypoint = function(point) {
    if($scope.waypoints.length > 8){
        var temp = [];
        temp.Push($scope.waypoints[0]); //Start Point
        temp.Push($scope.waypoints[7]); //Last point
        temp.Push(point); //New point
        $scope.waypoints = temp; //Replace the old object with this new one
    }
    else 
        $scope.waypoints.Push(point);
}
0
anupsahu