2016-08-03 85 views
-2

我已经签署了一个免费的谷歌地图api帐户来提取基于一个地方的名称的经度和纬度。现在,我设法获得了地点的坐标。不过,在Java中,有没有办法根据我从下面的程序中获得的经度和纬度来提取位置的城市名称?如何获得坐标为IN 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

什么是44行? –

回答

1

使用谷歌逆向地理编码为了将纬度/经度转换为地址,然后解析城市从JSON文件中返回的信息 - 城市被称为sublocality就像一个fyi。

阅读此处以获取更多信息:https://developers.google.com/maps/documentation/geocoding/start?csw=1

+0

我已经对我的问题做了一些编辑来指定我的困难,它能够解决吗?谢谢。 –

+0

@KennedyKan嘿,你能告诉我什么是第44行的参考? –

+0

你的意思是address_components?这是从url –

相关问题