如何计算Android中两个地图点之间的路径?

时间:2017-02-20 19:21:16

标签: android directions

我正在开发iOS / Android地图应用程序。

要在iOS中找到两个位置之间的路径,我使用MKDirectionsRequest

如何在Android中执行此操作?

我找到了 Directions API ,这是一项Web服务。所以我将不得不发送HTTP请求。

是否有任何Java接口可以在Android中计算路线?

1 个答案:

答案 0 :(得分:2)

将此设为答案。

您可以使用Android-GoogleDirectionLibrary

enter image description here

因此:

GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
        .from(new LatLng(37.7681994, -122.444538))
            .to(new LatLng(37.7749003,-122.4034934))
            .avoid(AvoidType.FERRIES)
            .avoid(AvoidType.HIGHWAYS)
            .execute(new DirectionCallback() {
    @Override
    public void onDirectionSuccess(Direction direction, String rawBody) {
        if(direction.isOK()) {
            // Do something
        } else {
            // Do something
        }
    }

    @Override
    public void onDirectionFailure(Throwable t) {
        // Do something
    }
});