Pu Zhibing
8 天以前 4c99ee7028c3fe58a2cd4b8273b22c75c45574fc
ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleMap/GoogleMapUtil.java
@@ -1,16 +1,18 @@
package com.stylefeng.guns.modular.system.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 com.stylefeng.guns.core.util.ToolUtil;
/**
 * 谷歌地图工具类
 */
public class GoogleMapUtil {
    private final static String key = "AIzaSyA_FEliOkbkL1IAHQsnBpbpo9MlIp729H0";
    private final static String key = "AIzaSyCG6PsfkaCEc94VK2vIAZk1YYKvOS_Ewts";
    /**
@@ -46,11 +48,14 @@
     * @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));
        if(ToolUtil.isNotEmpty(tripId)){
            request.header("X-Goog-Maps-Experience-ID", tripId);//行程id
        }
        GeocodingResult[] results = request.await();
        ReverseGeocodeVo vo = null;
        if(results.length > 0){
@@ -82,15 +87,20 @@
     * @return
     * @throws Exception
     */
    public static FindPlaceFromTextVo findplacefromtext(String input) throws Exception{
    public static FindPlaceFromTextVo findplacefromtext(String input, String tripId) throws Exception{
        GeoApiContext context = new GeoApiContext.Builder()
                .apiKey(key)
                .build();
        FindPlaceFromTextRequest request = new FindPlaceFromTextRequest(context);
        request.input(input);
        request.inputType(FindPlaceFromTextRequest.InputType.TEXT_QUERY);
        if(ToolUtil.isNotEmpty(tripId)){
            request.header("X-Goog-Maps-Experience-ID", tripId);//行程id
        }
        System.err.println("findplacefromtext(请求参数)--->" + input);
        FindPlaceFromText findPlaceFromText = request.await();
        PlacesSearchResult[] candidates = findPlaceFromText.candidates;
        System.err.println("findplacefromtext(返回结果)--->" + JSON.toJSONString(candidates));
        FindPlaceFromTextVo vo = null;
        if(candidates.length > 0){
            vo = new FindPlaceFromTextVo();
@@ -116,14 +126,18 @@
     * @return
     * @throws Exception
     */
    public static FindPlaceFromTextVo textsearch(String query) throws Exception{
    public static FindPlaceFromTextVo textsearch(String query, String tripId) throws Exception{
        GeoApiContext context = new GeoApiContext.Builder()
                .apiKey(key)
                .build();
        TextSearchRequest request = new TextSearchRequest(context);
        request.query(query);
        if(ToolUtil.isNotEmpty(tripId)){
            request.header("X-Goog-Maps-Experience-ID", tripId);//行程id
        }
        PlacesSearchResponse placesSearchResponse = request.await();
        PlacesSearchResult[] results = placesSearchResponse.results;
        System.err.println("textsearch--->" + JSON.toJSONString(results));
        FindPlaceFromTextVo vo = null;
        if(results.length > 0){
            vo = new FindPlaceFromTextVo();
@@ -151,12 +165,15 @@
     * @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.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));
@@ -179,13 +196,13 @@
     * @return
     * @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);
    public static DistancematrixVo getDistancematrix(Double sLat, Double sLnt, Double eLat, Double eLnt, String tripId) throws Exception{
        ReverseGeocodeVo reverseGeocode = getReverseGeocode(sLat, sLnt, tripId);
        ReverseGeocodeVo reverseGeocode1 = getReverseGeocode(eLat, eLnt, tripId);
        if(null != reverseGeocode && null != reverseGeocode1){
            String origin = reverseGeocode.getAddress();
            String destination = reverseGeocode1.getAddress();
            return getDistancematrix(origin, destination);
            return getDistancematrix(origin, destination, tripId);
        }
        return null;
    }
@@ -197,12 +214,15 @@
     * @param destination   终点 要计算方向的位置ID、地址或文本纬度/经度值。目标参数的选项与原点参数的相同。
     *
     */
    public static void getDirections(String origin, String destination) throws Exception{
    public static void getDirections(String origin, String destination, String tripId) throws Exception{
        GeoApiContext context = new GeoApiContext.Builder()
                .apiKey(key)
                .build();
        DirectionsApiRequest directions = DirectionsApi.getDirections(context, origin, destination);
        directions.mode(TravelMode.DRIVING);//出行方式(驾车)
        if(ToolUtil.isNotEmpty(tripId)){
            directions.header("X-Goog-Maps-Experience-ID", tripId);//行程id
        }
        DirectionsResult result = directions.await();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
@@ -212,14 +232,6 @@
    public static void main(String[] ages){
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }