| | |
| | | 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.*; |
| | |
| | | GeoApiContext context = new GeoApiContext.Builder() |
| | | .apiKey(key) |
| | | .build(); |
| | | GeocodingResult[] results = GeocodingApi.geocode(context, address).await(); |
| | | GeocodingApiRequest request = GeocodingApi.geocode(context, address); |
| | | request.language("en"); |
| | | GeocodingResult[] results = request.await(); |
| | | GeocodeVo vo = null; |
| | | if(results.length > 0){ |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(results[0].addressComponents)); |
| | | System.err.println(gson.toJson(results[0].addressComponents)); |
| | | |
| | | Geometry geometry = results[0].geometry; |
| | | LatLng location = geometry.location; |
| | |
| | | .apiKey(key) |
| | | .build(); |
| | | GeocodingApiRequest request = GeocodingApi.reverseGeocode(context, new LatLng(lat, lng)); |
| | | request.language("en"); |
| | | 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)); |
| | | |
| | | |
| | | System.err.println(gson.toJson(results[0].addressComponents)); |
| | | vo = new ReverseGeocodeVo(); |
| | | AddressComponent[] addressComponents = results[0].addressComponents; |
| | | AddressComponentsVo[] addressComponentsVos = new AddressComponentsVo[addressComponents.length]; |
| | |
| | | .apiKey(key) |
| | | .build(); |
| | | FindPlaceFromTextRequest request = new FindPlaceFromTextRequest(context); |
| | | request.language("en"); |
| | | 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; |
| | |
| | | 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.language("en"); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | .apiKey(key) |
| | | .build(); |
| | | DistanceMatrixApiRequest request = DistanceMatrixApi.getDistanceMatrix(context, new String[]{origin}, new String[]{destination}); |
| | | request.language("en"); |
| | | request.mode(TravelMode.DRIVING);//出行方式(驾车) |
| | | DistanceMatrix distanceMatrix = request.await(); |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.err.println(gson.toJson(distanceMatrix)); |
| | | context.shutdown(); |
| | | |
| | | DistanceMatrixElement elements = distanceMatrix.rows[0].elements[0]; |
| | | DistanceMatrixElementStatus status = elements.status; |
| | | DistancematrixVo vo = new DistancematrixVo(); |
| | | if(DistanceMatrixElementStatus.OK.equals(status)){ |
| | | vo.setDistance(elements.distance.inMeters); |
| | | vo.setDuration(elements.duration.inSeconds); |
| | | }else{ |
| | | vo.setDistance(0L); |
| | | vo.setDuration(0L); |
| | | } |
| | | 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(); |
| | |
| | | |
| | | |
| | | /** |
| | | * 获取两点之间的距离 |
| | | * @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{ |
| | | 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、地址或文本纬度/经度值。目标参数的选项与原点参数的相同。 |
| | |
| | | .apiKey(key) |
| | | .build(); |
| | | DirectionsApiRequest directions = DirectionsApi.getDirections(context, origin, destination); |
| | | directions.language("en"); |
| | | directions.mode(TravelMode.DRIVING);//出行方式(驾车) |
| | | DirectionsResult result = directions.await(); |
| | | |
| | | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| | | System.out.println(gson.toJson(result)); |
| | | System.err.println(gson.toJson(result)); |
| | | context.shutdown(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static void main(String[] ages){ |
| | | try { |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |