Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java示例进行反向地理编码_Java_Reverse Geocoding - Fatal编程技术网

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

使用java示例进行反向地理编码,java,reverse-geocoding,Java,Reverse Geocoding,使用java在两种策略中进行反向地理编码的java代码使用GeoApiContext和HttpClient 公共类ReverseGeoCoderUtil{ /** * Reverse Geocoding and returns formated Address . * eg:getFormatedAdress(40.714224, -73.961452); * * @param latitude latitude value *

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

公共类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;
    }        


}

有问题吗?
 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;
        }       


    }