Inaccurate Google Maps Elevation Service response when splitting a too large path

前端 未结 3 2101
情话喂你
情话喂你 2021-01-12 07:20

This is a bit of a question with some level of detail to it, so let me first explain the situation, then my implementation and last the question so you understand best.

3条回答
  •  醉梦人生
    2021-01-12 07:33

    The last remaining issue has also been solved with the help of this SO question: How to re-run a javascript promise when failed?. So if jfriend00 replies to this question I can award the bounty to him, since that's the trick that helped me out in the end.

    To be sure the function resolves at status OK, retries at OVER_QUERY_LIMIT and reject at any other status I had to put the Promise logic within a function and call that function, like so:

    function getRouteElevationChartDataBatchPromise(batch, batchSize) {
        return new Promise(function(resolve, reject) {
            function run(batch, batchSize) {
                var elevator = new google.maps.ElevationService();
                var thisBatchPath = [];
    
                for (var j = batch * batchSize; j < batch * batchSize + batchSize; j++) {
                    if (j < directions.routes[0].overview_path.length) {
                        thisBatchPath.push(directions.routes[0].overview_path[j]);
                    } else {
                        break;
                    }
                }
    
                elevator.getElevationAlongPath({
                    path: thisBatchPath,
                    samples: 512
                }, function (elevations, status) {
                    if(status == google.maps.ElevationStatus.OK) {
                        routeElevations = routeElevations.concat(elevations);
                        resolve();
                    } else if (status == google.maps.ElevationStatus.OVER_QUERY_LIMIT) {                        
                        setTimeout(function () {
                            run(batch, batchSize);
                        }, 200);
                    } else {
                        reject(status);
                    }
                });
            }
    
            run(batch, batchSize);
        });
    }
    

提交回复
热议问题