New file |
| | |
| | | package com.dsh.course.util.GoogleMap; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | import com.google.maps.*; |
| | | import com.google.maps.model.*; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.Instant; |
| | | |
| | | /** |
| | | * 谷歌地图工具类 |
| | | */ |
| | | @Component |
| | | public class GoogleMapUtil { |
| | | |
| | | private final String key = "AIzaSyBNgMRPVj8zt4OEGrUoyHifEDE9SCHYSLE"; |
| | | |
| | | |
| | | /** |
| | | * 地理编码(地址获取位置坐标) |
| | | * @param address 地址信息 |
| | | * @throws Exception |
| | | */ |
| | | public GeocodeVo getGeocode(String address) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | GeocodingResult[] results = GeocodingApi.geocode(context, address).await(); |
| | | GeocodeVo vo = null; |
| | | if(results.length > 0){ |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(results[0].addressComponents)); |
| | | |
| | | Geometry geometry = results[0].geometry; |
| | | LatLng location = geometry.location; |
| | | vo = new GeocodeVo(); |
| | | vo.setLat(location.lat); |
| | | vo.setLng(location.lng); |
| | | } |
| | | context.shutdown(); |
| | | return vo; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 逆地理编码获取地址信息 |
| | | * @param lat 纬度 |
| | | * @param lng 经度 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public ReverseGeocodeVo getReverseGeocode(double lat, double lng) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | GeocodingApiRequest request = GeocodingApi.reverseGeocode(context, new LatLng(lat, lng)); |
| | | GeocodingResult[] results = request.await(); |
| | | ReverseGeocodeVo vo = null; |
| | | if(results.length > 0){ |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(results[0].addressComponents)); |
| | | |
| | | |
| | | vo = new ReverseGeocodeVo(); |
| | | AddressComponent[] addressComponents = results[0].addressComponents; |
| | | AddressComponentsVo[] addressComponentsVos = new AddressComponentsVo[addressComponents.length]; |
| | | for (int i = 0; i < addressComponents.length; i++) { |
| | | AddressComponentsVo addressComponentsVos1 = new AddressComponentsVo(); |
| | | addressComponentsVos1.setLongName(addressComponents[i].longName); |
| | | addressComponentsVos1.setShortName(addressComponents[i].shortName); |
| | | addressComponentsVos[i] = addressComponentsVos1; |
| | | } |
| | | String address = results[0].formattedAddress; |
| | | vo.setAddressComponentsVos(addressComponentsVos); |
| | | vo.setAddress(address); |
| | | } |
| | | context.shutdown(); |
| | | return vo; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 搜索地图获取地图结果 |
| | | * @param input |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public FindPlaceFromTextVo findplacefromtext(String input) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | FindPlaceFromTextRequest request = new FindPlaceFromTextRequest(context); |
| | | request.input(input); |
| | | request.inputType(FindPlaceFromTextRequest.InputType.TEXT_QUERY); |
| | | FindPlaceFromText findPlaceFromText = request.await(); |
| | | PlacesSearchResult[] candidates = findPlaceFromText.candidates; |
| | | FindPlaceFromTextVo vo = null; |
| | | System.err.println(JSON.toJSONString(candidates)); |
| | | if(candidates.length > 0){ |
| | | vo = new FindPlaceFromTextVo(); |
| | | String formattedAddress = candidates[0].formattedAddress; |
| | | String name = candidates[0].name; |
| | | Geometry geometry = candidates[0].geometry; |
| | | if(null == geometry){//没有返回结果,使用place_id继续搜索 |
| | | // [{"permanentlyClosed":false,"placeId":"ChIJy1edVzvF7zYRaOqiGTkmb6I","rating":0.0,"userRatingsTotal":0}] |
| | | String placeId = candidates[0].placeId; |
| | | |
| | | |
| | | } |
| | | LatLng location = geometry.location; |
| | | double lat = location.lat; |
| | | double lng = location.lng; |
| | | |
| | | vo.setName(name); |
| | | vo.setAddress(formattedAddress); |
| | | vo.setLat(lat); |
| | | vo.setLng(lng); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 模糊搜索地图内容 |
| | | * @param query |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public FindPlaceFromTextVo textsearch(String query) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | TextSearchRequest request = new TextSearchRequest(context); |
| | | request.query(query); |
| | | PlacesSearchResponse placesSearchResponse = request.await(); |
| | | PlacesSearchResult[] results = placesSearchResponse.results; |
| | | FindPlaceFromTextVo vo = null; |
| | | System.err.println(JSON.toJSONString(results)); |
| | | if(results.length > 0){ |
| | | vo = new FindPlaceFromTextVo(); |
| | | String formattedAddress = results[0].formattedAddress; |
| | | String name = results[0].name; |
| | | Geometry geometry = results[0].geometry; |
| | | LatLng location = geometry.location; |
| | | double lat = location.lat; |
| | | double lng = location.lng; |
| | | |
| | | vo.setName(name); |
| | | vo.setAddress(formattedAddress); |
| | | vo.setLat(lat); |
| | | vo.setLng(lng); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取两个地点之间的预估里程和预估时间 |
| | | * @param origin 起点名称 |
| | | * @param destination 终点名称 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public DistancematrixVo getDistancematrix(String origin, String destination) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | DistanceMatrixApiRequest request = DistanceMatrixApi.getDistanceMatrix(context, new String[]{origin}, new String[]{destination}); |
| | | request.mode(TravelMode.DRIVING);//出行方式(驾车) |
| | | request.avoid(DirectionsApi.RouteRestriction.TOLLS); |
| | | request.trafficModel(TrafficModel.BEST_GUESS); |
| | | request.departureTime(Instant.now()); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取两点之间的距离 |
| | | * @param sLat |
| | | * @param sLnt |
| | | * @param eLat |
| | | * @param eLnt |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public DistancematrixVo getDistancematrix(Double sLat, Double sLnt, Double eLat, Double eLnt) throws Exception{ |
| | | 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);//出行方式(驾车) |
| | | request.avoid(DirectionsApi.RouteRestriction.TOLLS); |
| | | request.trafficModel(TrafficModel.BEST_GUESS); |
| | | request.departureTime(Instant.now()); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取两地点之间的线路规划 |
| | | * @param origin 起点 要计算方向的位置ID、地址或文本纬度/经度值。目标参数的选项与原点参数的相同。 |
| | | * @param destination 终点 要计算方向的位置ID、地址或文本纬度/经度值。目标参数的选项与原点参数的相同。 |
| | | * |
| | | */ |
| | | public void getDirections(String origin, String destination) throws Exception{ |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | DirectionsApiRequest directions = DirectionsApi.getDirections(context, origin, destination); |
| | | directions.mode(TravelMode.BICYCLING);//出行方式(骑行) |
| | | DirectionsResult result = directions.await(); |
| | | |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(result)); |
| | | context.shutdown(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |