使用Google Maps API如何确定两个位置之间的行驶距离?平台-Java
您将需要使用 Google Directions Web Service。您可以发出 HTTP 请求,指定两个位置作为参数,然后获取描述两点之间方向的 JSON(或 XML)。
NB. Google 地图服务条款 明确规定,禁止在不将结果发布到 Google 地图上的情况下使用 Google 地图网络服务。
我是 Java 新手,也遇到了同样的问题。这就是为什么我决定创建一个 Java 库来提供这些信息以及更多类似路线的信息。
这是链接。告诉我您对此有何看法以及它是否对您的案例有用。
使用路线 API: http://code.google.com/apis/maps/documentation/directions/
默认出行模式为驾驶。
请阅读服务条款: http://code.google.com/apis/maps/terms.html
特别是第 10.12 节 - 它指的是使用此 API 的结果。简而言之,必须结合Google地图来显示。
您可以使用 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;
}
}