我已经签署了一个免费的谷歌地图api帐户,根据地点名称提取纬度和经度。现在,我已设法获得这些地方的坐标。然而,Java中有没有办法根据我从下面的程序中获得的纬度和经度仅提取位置的城市名称?
修改
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Geolocation_City {
public static void main(String[] args) {
// making url request
try {
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true");
// making connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
// Reading data's from url
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
String out="";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
//System.out.println(output);
out+=output;
}
// Converting Json formatted string into JSON object
JsonParser parser = new JsonParser();
JsonObject json = (JsonObject) parser.parse(out);
JsonArray results = json.getAsJsonArray("results");
//codes for getting sublocality...
System.out.println(results);
conn.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以下是System.out.println(结果)的输出:
[{"address_components":[{"long_name":"277","short_name":"277","types":["street_number"]},{"long_name":"Bedford Avenue","short_name":"Bedford Ave","types":["route"]},{"long_name":"Williamsburg","short_name":"Williamsburg","types":["neighborhood","political"]},{"long_name":"Brooklyn","short_name":"Brooklyn","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Kings County","short_name":"Kings County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"11211","short_name":"11211","types":["postal_code"]}],"formatted_address":"277 Bedford Ave, Brooklyn, NY 11211, USA","geometry":{"location":{"lat":40.714232,"lng":-73.9612889},"location_type":"ROOFTOP","viewport":{"northeast":{"lat":40.7155809802915,"lng":-73.9599399197085},"southwest":{"lat":40.7128830197085,"lng":-73.96263788029151}}},"place_id":"ChIJd8BlQ2BZwokRAFUEcm_qrcA","types":["street_address"]},{"address_components":[{"long_name":"Grand St/Bedford Av","short_name":"Grand St/Bedford Av","types":["establishment","point_of_interest"]},{"long_name":"Williamsburg","short_name":"Williamsburg","types":["neighborhood","political"]},{"long_name":"Brooklyn","short_name":"Brooklyn","types":["political","sublocality","sublocality_level_1"]},{"long_name":"Kings County","short_name":"Kings County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"11211","short_name":"11211","types":["postal_code"]}],"formatted_address":"Grand St/Bedford Av, Brooklyn, NY 11211, USA","geometry":{"location":{"lat":40.714321,"lng":-73.961151},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":40.71566998029149,"lng":-73.95980201970849},"southwest":{"lat":40.7129720197085,"lng":-73.96249998029151}}},"place_id":"ChIJi27VXGBZwokRM8ErPyB91yk","types":["bus_station","establishment","point_of_interest","transit_station"]},{"address_components":[{"long_name":"Williamsburg","short_name":"Williamsburg","types":["neighborhood","political"]},{"long_name":"Brooklyn","short_name":"Brooklyn","types":["political","sublocality","sublocality_level_1"]},{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"Kings County","short_name":"Kings County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Williamsburg, Brooklyn, NY, USA","geometry":{"bounds":{"northeast":{"lat":40.7251773,"lng":-73.936498},"southwest":{"lat":40.6979329,"lng":-73.96984499999999}},"location":{"lat":40.7081156,"lng":-73.9570696},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":40.7251773,"lng":-73.936498},"southwest":{"lat":40.6979329,"lng":-73.96984499999999}}},"place_id":"ChIJQSrBBv1bwokRbNfFHCnyeYI","types":["neighborhood","political"]},{"address_components":[{"long_name":"Brooklyn","short_name":"Brooklyn","types":["political","sublocality","sublocality_level_1"]},{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"Kings County","short_name":"Kings County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Brooklyn, NY, USA","geometry":{"bounds":{"northeast":{"lat":40.739446,"lng":-73.83336509999999},"southwest":{"lat":40.551042,"lng":-74.05663}},"location":{"lat":40.6781784,"lng":-73.94415789999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":40.739446,"lng":-73.83336509999999},"southwest":{"lat":40.551042,"lng":-74.05663}}},"place_id":"ChIJCSF8lBZEwokRhngABHRcdoI","types":["political","sublocality","sublocality_level_1"]},{"address_components":[{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"New York, NY, USA","geometry":{"bounds":{"northeast":{"lat":40.9152555,"lng":-73.70027209999999},"southwest":{"lat":40.496044,"lng":-74.255735}},"location":{"lat":40.7127837,"lng":-74.0059413},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":40.9152555,"lng":-73.70027209999999},"southwest":{"lat":40.496044,"lng":-74.255735}}},"place_id":"ChIJOwg_06VPwokRYv534QaPC8g","types":["locality","political"]},{"address_components":[{"long_name":"11211","short_name":"11211","types":["postal_code"]},{"long_name":"Brooklyn","short_name":"Brooklyn","types":["political","sublocality","sublocality_level_1"]},{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Brooklyn, NY 11211, USA","geometry":{"bounds":{"northeast":{"lat":40.7280089,"lng":-73.9207299},"southwest":{"lat":40.7008331,"lng":-73.9644697}},"location":{"lat":40.7093358,"lng":-73.9565551},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":40.7280089,"lng":-73.9207299},"southwest":{"lat":40.7008331,"lng":-73.9644697}}},"place_id":"ChIJvbEjlVdZwokR4KapM3WCFRw","types":["postal_code"]},{"address_components":[{"long_name":"Kings County","short_name":"Kings County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Kings County, NY, USA","geometry":{"bounds":{"northeast":{"lat":40.7391407,"lng":-73.83336509999999},"southwest":{"lat":40.5703742,"lng":-74.04195919999999}},"location":{"lat":40.6528762,"lng":-73.95949399999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":40.7391407,"lng":-73.83336509999999},"southwest":{"lat":40.5703742,"lng":-74.04195919999999}}},"place_id":"ChIJOwE7_GTtwokRs75rhW4_I6M","types":["administrative_area_level_2","political"]},{"address_components":[{"long_name":"New York-Northern New Jersey-Long Island, NY-NJ-PA","short_name":"New York-Northern New Jersey-Long Island, NY-NJ-PA","types":["political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"New York-Northern New Jersey-Long Island, NY-NJ-PA, USA","geometry":{"bounds":{"northeast":{"lat":41.6018065,"lng":-71.85621399999999},"southwest":{"lat":39.49853299999999,"lng":-75.3585939}},"location":{"lat":40.9590293,"lng":-74.0300122},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":41.6018065,"lng":-71.85621399999999},"southwest":{"lat":39.49853299999999,"lng":-75.3585939}}},"place_id":"ChIJ3YJV4PRWwokRFFI21ZrHXtQ","types":["political"]},{"address_components":[{"long_name":"New York Metropolitan Area","short_name":"New York Metropolitan Area","types":["political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"New York Metropolitan Area, USA","geometry":{"bounds":{"northeast":{"lat":42.0809059,"lng":-71.777491},"southwest":{"lat":39.475198,"lng":-75.3587649}},"location":{"lat":40.7127761,"lng":-74.00595439999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":42.0809059,"lng":-71.777491},"southwest":{"lat":39.475198,"lng":-75.3587649}}},"place_id":"ChIJ-5Z24NaGwokRiMh4Rj8FNMo","types":["political"]},{"address_components":[{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"New York, USA","geometry":{"bounds":{"northeast":{"lat":45.0158651,"lng":-71.8562294},"southwest":{"lat":40.4960911,"lng":-79.7621439}},"location":{"lat":43.2994285,"lng":-74.21793260000001},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":45.0156271,"lng":-71.8562294},"southwest":{"lat":40.4960911,"lng":-79.7621439}}},"place_id":"ChIJqaUj8fBLzEwRZ5UY3sHGz90","types":["administrative_area_level_1","establishment","point_of_interest","political"]},{"address_components":[{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"United States","geometry":{"bounds":{"northeast":{"lat":71.3867745,"lng":-66.9502861},"southwest":{"lat":18.9106771,"lng":172.4458954}},"location":{"lat":37.09024,"lng":-95.712891},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":49.38,"lng":-66.94},"southwest":{"lat":25.82,"lng":-124.39}}},"place_id":"ChIJCzYy5IS16lQRQrfeQ5K5Oxw","types":["country","political"]}]
如果我只想提取城市名称,即sublocality,我应该如何修改代码?
答案 0 :(得分:1)
使用Google Reverse地理编码将lat / long转换为地址,然后只需从JSON文件中返回的信息解析城市 - city就像fyi一样被称为sublocality
。
阅读本文以获取更多信息:https://developers.google.com/maps/documentation/geocoding/start?csw=1