package com.stylefeng.guns.modular.system.util.GoogleMap;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.maps.*;
|
import com.google.maps.model.*;
|
|
/**
|
* 谷歌地图工具类
|
*/
|
public class GoogleMapUtil {
|
|
private final static String key = "AIzaSyCG6PsfkaCEc94VK2vIAZk1YYKvOS_Ewts";
|
|
|
/**
|
* 地理编码(地址获取位置坐标)
|
* @param address 地址信息
|
* @throws Exception
|
*/
|
public static 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 static 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 addressComponentsVo = new AddressComponentsVo();
|
addressComponentsVo.setLongName(addressComponents[i].longName);
|
addressComponentsVo.setShortName(addressComponents[i].shortName);
|
addressComponentsVos[i] = addressComponentsVo;
|
}
|
String address = results[0].formattedAddress;
|
vo.setAddressComponentsVos(addressComponentsVos);
|
vo.setAddress(address);
|
}
|
context.shutdown();
|
return vo;
|
}
|
|
|
/**
|
* 搜索地图获取地图结果
|
* @param input
|
* @return
|
* @throws Exception
|
*/
|
public static 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;
|
if(candidates.length > 0){
|
vo = new FindPlaceFromTextVo();
|
String formattedAddress = candidates[0].formattedAddress;
|
String name = candidates[0].name;
|
Geometry geometry = candidates[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 query
|
* @return
|
* @throws Exception
|
*/
|
public static 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;
|
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 static 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);//出行方式(驾车)
|
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 static 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);//出行方式(驾车)
|
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];
|
DistanceMatrixElementStatus status = elements.status;
|
if(status.compareTo(DistanceMatrixElementStatus.OK) != 0){
|
return null;
|
}
|
DistancematrixVo vo = new DistancematrixVo();
|
vo.setDistance(elements.distance.inMeters);
|
vo.setDuration(elements.duration.inSeconds);
|
return vo;
|
}
|
|
|
/**
|
* 获取两地点之间的线路规划
|
* @param origin 起点 要计算方向的位置ID、地址或文本纬度/经度值。目标参数的选项与原点参数的相同。
|
* @param destination 终点 要计算方向的位置ID、地址或文本纬度/经度值。目标参数的选项与原点参数的相同。
|
*
|
*/
|
public static 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();
|
}
|
|
|
|
|
|
public static void main(String[] ages){
|
try {
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
|
}
|