【问题标题】:Android Directions API ArrayIndexOutOfBoundsExceptionAndroid Directions API ArrayIndexOutOfBoundsException
【发布时间】:2020-06-16 16:00:33
【问题描述】:

我正在使用 Google Directions API。在我将 TravelMode 设置为 BICYCLING 之前,一切正常。 在设置此属性之前,所有其他模式均有效。

我得到的错误是:onFailure: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

为什么会出现这个错误?

代码如下:

    private void calculateDirections(Marker marker){
        Log.d(TAG, "calculateDirections: calculating directions.");

        com.google.maps.model.LatLng destination = new com.google.maps.model.LatLng(
                marker.getPosition().latitude,
                marker.getPosition().longitude
        );
        DirectionsApiRequest directions = new DirectionsApiRequest(mGeoApiContext);
        directions  .alternatives(true)
                    .origin(new com.google.maps.model.LatLng(
                        deviceLocation.getLatitude(),
                        deviceLocation.getLongitude()));


        // Set imperial and transport mode
        try {
            if (imperial) {
                directions.units(Unit.IMPERIAL);
                Log.d(TAG, "calculateDirections: imperial set true");
            }
            switch (transportMode){
                case "Drive":
                    directions.mode(TravelMode.DRIVING);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
                case "Walk":
                    directions.mode(TravelMode.WALKING);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
                case "Transit":
                    directions.mode(TravelMode.TRANSIT);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
                case "Cycle":
                    directions.mode(TravelMode.BICYCLING);
                    Log.d(TAG, "calculateDirections: travelMode: " + transportMode);
                    break;
            }
        } catch (Exception e){
            Log.e(TAG, "calculateDirections: imperial error: " + e.getMessage());
        }

        Log.d(TAG, "calculateDirections: destination: " + destination.toString());
        directions.destination(destination).setCallback(new PendingResult.Callback<DirectionsResult>() {
            @Override
            public void onResult(DirectionsResult result) {
                Log.d(TAG, "calculateDirections: onResult: routes: " + result.routes[0].toString());
                Log.d(TAG, "calculateDirections: onResult: duration: " + result.routes[0].legs[0].duration);
                Log.d(TAG, "calculateDirections: onResult: distance: " + result.routes[0].legs[0].distance);
                Log.d(TAG, "calculateDirections: onResult: geocodedWayPoints: " + result.geocodedWaypoints[0].toString());

                addPolyLinesToMap(result);
            }

            @Override
            public void onFailure(Throwable e) {
                Log.e(TAG, "calculateDirections: onFailure: " + e ); 

            }
        });
    }

【问题讨论】:

    标签: java android api directions


    【解决方案1】:

    这绝对是由于该旅行模式没有路线造成的(根据 Google Direction Api)。使用前必须检查result.routes 是否为空。

    public void onResult(DirectionsResult result) {
             if (result.routes!=null&&result.routes.length>0){
                 //draw polylines
             } else{
                 //no route found
             }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多