| | |
| | | * @throws Exception |
| | | */ |
| | | public static DistancematrixVo getDistancematrix(Double sLat, Double sLnt, Double eLat, Double eLnt) throws Exception{ |
| | | ReverseGeocodeVo reverseGeocode = getReverseGeocode(sLat, sLnt); |
| | | ReverseGeocodeVo reverseGeocode1 = getReverseGeocode(eLat, eLnt); |
| | | if(null != reverseGeocode && null != reverseGeocode1){ |
| | | String origin = reverseGeocode.getAddress(); |
| | | String destination = reverseGeocode1.getAddress(); |
| | | return getDistancematrix(origin, destination); |
| | | } |
| | | return null; |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | DistanceMatrixApiRequest request = DistanceMatrixApi.newRequest(context); |
| | | request.origins(new LatLng(sLat, sLnt)); |
| | | request.destinations(new LatLng(eLat, eLnt)); |
| | | request.mode(TravelMode.DRIVING);//出行方式(驾车) |
| | | DistanceMatrix distanceMatrix = request.await(); |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(distanceMatrix)); |
| | | context.shutdown(); |
| | | |
| | | DistanceMatrixElement elements = distanceMatrix.rows[0].elements[0]; |
| | | DistancematrixVo vo = new DistancematrixVo(); |
| | | vo.setDistance(elements.distance.inMeters); |
| | | vo.setDuration(elements.duration.inSeconds); |
| | | return vo; |
| | | } |
| | | |
| | | |