Google 地图 API - 两个地址之间的距离

问题描述 投票:0回答:4

使用Google Maps API如何确定两个位置之间的行驶距离?平台-Java

java google-maps
4个回答
14
投票

您将需要使用 Google Directions Web Service。您可以发出 HTTP 请求,指定两个位置作为参数,然后获取描述两点之间方向的 JSON(或 XML)。

NB. Google 地图服务条款 明确规定,禁止在不将结果发布到 Google 地图上的情况下使用 Google 地图网络服务。


1
投票

我是 Java 新手,也遇到了同样的问题。这就是为什么我决定创建一个 Java 库来提供这些信息以及更多类似路线的信息。

这是链接。告诉我您对此有何看法以及它是否对您的案例有用。


0
投票

使用路线 API: http://code.google.com/apis/maps/documentation/directions/

默认出行模式为驾驶。

请阅读服务条款: http://code.google.com/apis/maps/terms.html

特别是第 10.12 节 - 它指的是使用此 API 的结果。简而言之,必须结合Google地图来显示。


0
投票

您可以使用 Java Google Maps SDK :

    <dependency>
        <groupId>com.google.maps</groupId>
        <artifactId>google-maps-services</artifactId>
        <version>2.2.0</version>
    </dependency>

代码:

     /**
     *
     * @param apiKey
     * @param origin
     * @param destination
     * @return
     */
    public static DistanceMatrixRow[] getDistance(String apiKey, String origin, String destination)
    {

        DistanceMatrixApiRequest req = DistanceMatrixApi.newRequest(new GeoApiContext.Builder()
                .apiKey(apiKey)
                .build());

        DistanceMatrix query = req.origins(origin)
                .destinations(destination)
                .mode(TravelMode.DRIVING)
                .avoid(RouteRestriction.TOLLS)
                .language("en-US")
                .awaitIgnoreError();

        if (query != null && query.rows.length > 0)
        {
            return query.rows;
        } else
        {
            return null;
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.