gpt4 book ai didi

java - 如何使用Google Maps Distance Matrix JAVA API获取源和多个目的地之间的最近距离

转载 作者:行者123 更新时间:2023-11-30 06:08:02 28 4
gpt4 key购买 nike

我添加了 Google Maps API Java Client 的依赖在我的 Java 项目中。我有一组起点距离、终点距离以及 GeoApiContext 本身:

GeoApiContext context = new GeoApiContext().setApiKey(MY_API_KEY);
String[] destinationAddress = {"40.7127837,-74.0059413", "33.9533487,-117.3961564", "38.6270025,-90.19940419999999"};
String[] originAddress = {"12.8445,80.1523"};

我想获得这些点之间的(飞机?)距离。我知道我们可以发送一个简单的 HTTP 请求,例如
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.690515%73.6334271&key= YOUR_API_KEY

但我想使用 JAVA Google Maps API。我尝试执行 DistanceMatrixApiRequest s = DistanceMatrixApi.getDistanceMatrix(context, originAddress, destinationAddress); 但没有 getArrivalTimesgetDistanceMatrix 或无论如何发送请求。我很困扰。请帮助。谢谢

最佳答案

试试这个:

private static final String API_KEY = "YOUR_API_KEY";
private static final GeoApiContext context = new GeoApiContext().setApiKey(API_KEY);


public DistanceMatrix estimateRouteTime(DateTime time, Boolean isForCalculateArrivalTime, DirectionsApi.RouteRestriction routeRestriction, LatLng departure, LatLng... arrivals) {
try {
DistanceMatrixApiRequest req = DistanceMatrixApi.newRequest(context);
if (isForCalculateArrivalTime) {
req.departureTime(time);
} else {
req.arrivalTime(time);
}
if (routeRestriction == null) {
routeRestriction = DirectionsApi.RouteRestriction.TOLLS;
}
DistanceMatrix trix = req.origins(departure)
.destinations(arrivals)
.mode(TravelMode.DRIVING)
.avoid(routeRestriction)
.language("fr-FR")
.await();
return trix;

} catch (ApiException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}

DistanceMatrix 响应对象是这样的:

{
"destination_addresses" : [ "San Francisco, Californie, États-Unis" ],
"origin_addresses" : [ "Seattle, Washington, États-Unis" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1 300 km",
"value" : 1299878
},
"duration" : {
"text" : "12 heures 32 minutes",
"value" : 45146
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

关于java - 如何使用Google Maps Distance Matrix JAVA API获取源和多个目的地之间的最近距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40561264/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com