简体   繁体   中英

Draw driving route on google maps

i'm working on an Android geolocation app, and i'm using the com.akexorcist:googledirectionlibrary:1.0.4 library. I used to draw routes using this code

public void requestDirection(final LatLng origin, final LatLng destination) {
    Toast.makeText(getBaseContext(), "Requesting direction, just a moment...", Toast.LENGTH_SHORT).show();
    GoogleDirection.withServerKey(SERVER_KEY)
            .from(origin)
            .to(destination)
            .transportMode(TransportMode.DRIVING)
            .execute(new DirectionCallback() {
                @Override
                public void onDirectionSuccess(Direction direction, String rawBody) {
                    if (direction.isOK()) {
                        ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();

                        if (!colorBlue && !path){
                            mGoogleMap.addPolyline(DirectionConverter.createPolyline(getApplicationContext(), directionPositionList, 5, Color.RED));
                        }
                        else if (colorBlue){
                            mGoogleMap.addPolyline(DirectionConverter.createPolyline(getApplicationContext(), directionPositionList, 5, Color.BLUE));
                            colorBlue = false;
                        }else if (cancelled){
                            mGoogleMap.addPolyline(DirectionConverter.createPolyline(getApplicationContext(), directionPositionList, 5, Color.GREEN));
                            cancelled = false;
                        }
                    }else  Toast.makeText(getBaseContext(), "Route suspicious", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onDirectionFailure(Throwable t) {
                    Toast.makeText(getBaseContext(), "An error occured!", Toast.LENGTH_SHORT).show();
                }
            });
}

but now i always get the Route suspicious message. After debugging i noticed that directionPositionList is always null even if the origin and destination aren't. Any ideas how can i solve this problem ? Thanks in advance

问题出在Google ServerKey已过期,所以在更改密钥后,我能够再次绘制路线。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM