package com.stylefeng.guns.modular.system.utils.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 com.stylefeng.guns.modular.system.utils.HttpClientUtil; import com.stylefeng.guns.modular.system.utils.HttpResult; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * 谷歌地图工具类 */ @Slf4j @Component public class GoogleMapUtil { private static final String key = "AIzaSyBBW0XxW1FK7IXmmS7KFtAjX3o99eFPsss"; @Autowired private HttpClientUtil httpClientUtil; /** * 地理编码(地址获取位置坐标) * @param address 地址信息 * @throws Exception */ public static GeocodeVo getGeocode(String address){ GeoApiContext context = new GeoApiContext.Builder() .apiKey(key) .build(); GeocodingResult[] results = null; try{ results = GeocodingApi.geocode(context, address).await(); }catch (Exception e){ e.printStackTrace(); log.error("地址解析经纬度失败!"); } 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{ // TODO: 2022/10/22 临时用IGO跳转 // Map map = new HashMap<>(); // map.put("lat", lat); // map.put("lng", lng); // map.put("key", key); // HttpResult httpResult = httpClientUtil.pushHttpRequset("POST", "http://182.160.16.251:1010/base/googleMap/getReverseGeocode", map, null, "form"); // ReverseGeocodeVo reverseGeocode = JSON.parseObject(httpResult.getData(), ReverseGeocodeVo.class); // return reverseGeocode; 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{ // TODO: 2022/10/22 临时用IGO跳转 // Map map = new HashMap<>(); // map.put("query", query); // map.put("key", key); // HttpResult httpResult = httpClientUtil.pushHttpRequset("POST", "http://182.160.16.251:1010/base/googleMap/textsearch", map, null, "form"); // FindPlaceFromTextVo findPlaceFromTextVo = JSON.parseObject(httpResult.getData(), FindPlaceFromTextVo.class); // return findPlaceFromTextVo; 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{ // TODO: 2022/10/22 临时用IGO跳转 // Map map = new HashMap<>(); // map.put("origin", origin); // map.put("destination", destination); // map.put("key", key); // HttpResult httpResult = httpClientUtil.pushHttpRequset("POST", "http://182.160.16.251:1010/base/googleMap/getDistancematrix", map, null, "form"); // DistancematrixVo reverseGeocode = JSON.parseObject(httpResult.getData(), DistancematrixVo.class); // return reverseGeocode; GeoApiContext context = new GeoApiContext.Builder() .apiKey(key) .build(); DistanceMatrixApiRequest request = DistanceMatrixApi.getDistanceMatrix(context, new String[]{origin}, new String[]{destination}); 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; } /** * 获取两点之间的距离 * @param sLat * @param sLnt * @param eLat * @param eLnt * @return * @throws Exception */ public 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; } /** * 获取两地点之间的线路规划 * @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.DRIVING);//出行方式(驾车) DirectionsResult result = directions.await(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); System.out.println(gson.toJson(result)); context.shutdown(); } }