使用java示例反向地理编码

问题描述 投票:-5回答:1

使用GeoApiContext和HttpClient在两个stratergires中使用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;
    }        


}
java reverse-geocoding
1个回答
-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;
        }       


    }
© www.soinside.com 2019 - 2024. All rights reserved.