| | |
| | | import com.google.gson.GsonBuilder; |
| | | import com.google.maps.*; |
| | | import com.google.maps.model.*; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | |
| | | /** |
| | | * 谷歌地图工具类 |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static ReverseGeocodeVo getReverseGeocode(double lat, double lng) throws Exception{ |
| | | public static ReverseGeocodeVo getReverseGeocode(double lat, double lng, String tripId) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | GeocodingApiRequest request = GeocodingApi.reverseGeocode(context, new LatLng(lat, lng)); |
| | | request.language("en"); |
| | | if(ToolUtil.isNotEmpty(tripId)){ |
| | | request.header("X-Goog-Maps-Experience-ID", tripId);//行程id |
| | | } |
| | | GeocodingResult[] results = request.await(); |
| | | ReverseGeocodeVo vo = null; |
| | | if(results.length > 0){ |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static DistancematrixVo getDistancematrix(String origin, String destination) throws Exception{ |
| | | public static DistancematrixVo getDistancematrix(String origin, String destination, String tripId) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | DistanceMatrixApiRequest request = DistanceMatrixApi.getDistanceMatrix(context, new String[]{origin}, new String[]{destination}); |
| | | request.language("en"); |
| | | request.mode(TravelMode.DRIVING);//出行方式(驾车) |
| | | if(ToolUtil.isNotEmpty(tripId)){ |
| | | request.header("X-Goog-Maps-Experience-ID", tripId);//行程id |
| | | } |
| | | DistanceMatrix distanceMatrix = request.await(); |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.err.println(gson.toJson(distanceMatrix)); |
| | |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static DistancematrixVo getDistancematrix(Double sLat, Double sLnt, Double eLat, Double eLnt) throws Exception{ |
| | | public static DistancematrixVo getDistancematrix(Double sLat, Double sLnt, Double eLat, Double eLnt, String tripId) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | |
| | | request.origins(new LatLng(sLat, sLnt)); |
| | | request.destinations(new LatLng(eLat, eLnt)); |
| | | request.mode(TravelMode.DRIVING);//出行方式(驾车) |
| | | if(ToolUtil.isNotEmpty(tripId)){ |
| | | request.header("X-Goog-Maps-Experience-ID", tripId);//行程id |
| | | } |
| | | DistanceMatrix distanceMatrix = request.await(); |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(distanceMatrix)); |