使用Java示例进行反向地理编码

时间:2019-03-14 11:17:58

标签: java reverse-geocoding

使用GeoApiContext和HttpClient在两个策略中使用Java进行反向地理编码的Java代码。

公共类ReverseGeoCoderUtil {

    /**
     * Reverse Geocoding and returns formated Address .
     * eg:getFormatedAdress(40.714224, -73.961452);
     * 
     * @param latitude     latitude value
     * @param longitude    longitude value
     * @param googleApiKey
     * @throws Exception if a reverse geocoding error occurred
     * @return formated Address
     */
    public static String getFormatedAdress(double latitude, double longitude, String googleApiKey) throws Exception 
    {
        GeoApiContext context = new GeoApiContext.Builder().apiKey(googleApiKey).build();
        String name = "(Unknown)";
        try {
            GeocodingResult[] results = GeocodingApi.reverseGeocode(context, new LatLng(latitude, longitude)).await();
            for (GeocodingResult result : results) {
                return result.formattedAddress;
            }
        } catch (Exception e) {
            throw new Exception("Error on Reverse Geocoding");
        }

        return name;
    }        


}

1 个答案:

答案 0 :(得分:-1)

 public class ReverseGeoCoderUtil {

        /**
         * Reverse Geocoding and returns formated Address .
         * eg:getFormatedAdress(40.714224, -73.961452);
         * 
         * @param latitude     latitude value
         * @param longitude    longitude value
         * @param googleApiKey
         * @throws Exception if a reverse geocoding error occurred
         * @return formated Address
         */
        public static String getFormatedAdress(double latitude, double longitude, String googleApiKey) throws Exception 
        {
            GeoApiContext context = new GeoApiContext.Builder().apiKey(googleApiKey).build();
            String name = "(Unknown)";
            try {
                GeocodingResult[] results = GeocodingApi.reverseGeocode(context, new LatLng(latitude, longitude)).await();
                for (GeocodingResult result : results) {
                    return result.formattedAddress;
                }
            } catch (Exception e) {
                throw new Exception("Error on Reverse Geocoding");
            }

            return name;
        }


        /**
         * Reverse Geocoding and returns formated Address .
         * eg:getFormatedAdress(40.714224, -73.961452);
         * 
         * @param latitude     latitude value
         * @param longitude    longitude value
         * @param googleApiKey
         * @throws Exception if a reverse geocoding error occurred
         * @return formated Address
         */
        public static String getFormatedAdress2(double latitude, double longitude, String googleApiKey) throws Exception {
            InputStream inputStream = null;
            String json = "";
            String formatedAdress = "";
            try {

                String apiUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude
                        + "&key=" + googleApiKey;
                HttpPost post = new HttpPost(apiUrl);
                HttpClient client = new DefaultHttpClient();
                HttpResponse response = client.execute(post);
                HttpEntity entity = response.getEntity();
                inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
                StringBuilder sbuild = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sbuild.append(line);
                }
                inputStream.close();
                json = sbuild.toString();
            } catch (Exception e) {
                throw new Exception("Error on Reverse Geocoding");
            }
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(json);
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray resultArray = (JSONArray) jsonObject.get("results");
            JSONObject addressJsonObject = (JSONObject) resultArray.get(0);
            formatedAdress = addressJsonObject.get("formatted_address").toString();
            return formatedAdress;
        }       


    }