使用html到java,如何从坐标获取街道名称?

时间:2015-09-17 00:07:06

标签: java google-maps coordinates google-earth

我拥有UTM的纬度和经度,我需要使用它们来获取街道名称。

我正在使用Java。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Google地图反向地理编码从坐标中获取街道名称。

https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

例如,下面的代码(我使用了google文档中的latlng):

public class Test {
    public static void main(String[] args) {
        try {
            URL url = new URL(
                    "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452");
            URLConnection con = url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String line = br.readLine();
            while (line != null) {
                System.out.println(line);
                line = br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

打印出来:

    {
           "results" : [
              {
                 "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" : [ "sublocality_level_1", "sublocality", "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" ]
                    },
                    {
                       "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" : [ "point_of_interest", "establishment" ]
                    },
                    {
                       "long_name" : "Williamsburg",
                       "short_name" : "Williamsburg",
                       "types" : [ "neighborhood", "political" ]
                    },
                    {
                       "long_name" : "Brooklyn",
                       "short_name" : "Brooklyn",
                       "types" : [ "sublocality_level_1", "sublocality", "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" ]
                    },
                    {
                       "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",
                    "transit_station",
                    "point_of_interest",
                    "establishment"
                 ]
              },
              {
                 "address_components" : [
                    {
                       "long_name" : "Williamsburg",
                       "short_name" : "Williamsburg",
                       "types" : [ "neighborhood", "political" ]
                    },
                    {
                       "long_name" : "Brooklyn",
                       "short_name" : "Brooklyn",
                       "types" : [ "sublocality_level_1", "sublocality", "political" ]
                    },
                    {
                       "long_name" : "New York",
                       "short_name" : "NY",
                       "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" : [ "sublocality_level_1", "sublocality", "political" ]
                    },
                    {
                       "long_name" : "New York",
                       "short_name" : "NY",
                       "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" : [ "sublocality_level_1", "sublocality", "political" ]
              },
              {
                 "address_components" : [
                    {
                       "long_name" : "New York",
                       "short_name" : "NY",
                       "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.91525559999999,
                          "lng" : -73.70027209999999
                       },
                       "southwest" : {
                          "lat" : 40.4913699,
                          "lng" : -74.25908989999999
                       }
                    },
                    "location" : {
                       "lat" : 40.7127837,
                       "lng" : -74.0059413
                    },
                    "location_type" : "APPROXIMATE",
                    "viewport" : {
                       "northeast" : {
                          "lat" : 40.91525559999999,
                          "lng" : -73.70027209999999
                       },
                       "southwest" : {
                          "lat" : 40.4913699,
                          "lng" : -74.25908989999999
                       }
                    }
                 },
                 "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" : [ "sublocality_level_1", "sublocality", "political" ]
                    },
                    {
                       "long_name" : "New York",
                       "short_name" : "NY",
                       "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.739446,
                          "lng" : -73.83336509999999
                       },
                       "southwest" : {
                          "lat" : 40.5702197,
                          "lng" : -74.04195919999999
                       }
                    },
                    "location" : {
                       "lat" : 40.6528762,
                       "lng" : -73.95949399999999
                    },
                    "location_type" : "APPROXIMATE",
                    "viewport" : {
                       "northeast" : {
                          "lat" : 40.739446,
                          "lng" : -73.83336509999999
                       },
                       "southwest" : {
                          "lat" : 40.5702197,
                          "lng" : -74.04195919999999
                       }
                    }
                 },
                 "place_id" : "ChIJOwE7_GTtwokRs75rhW4_I6M",
                 "types" : [ "administrative_area_level_2", "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.015865,
                          "lng" : -71.8562755
                       },
                       "southwest" : {
                          "lat" : 40.4913686,
                          "lng" : -79.76214379999999
                       }
                    },
                    "location" : {
                       "lat" : 43.2994285,
                       "lng" : -74.21793260000001
                    },
                    "location_type" : "APPROXIMATE",
                    "viewport" : {
                       "northeast" : {
                          "lat" : 45.0156272,
                          "lng" : -71.8562755
                       },
                       "southwest" : {
                          "lat" : 40.4913686,
                          "lng" : -79.76214379999999
                       }
                    }
                 },
                 "place_id" : "ChIJqaUj8fBLzEwRZ5UY3sHGz90",
                 "types" : [ "administrative_area_level_1", "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.9110642,
                          "lng" : 172.4458955
                       }
                    },
                    "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" ]
              }
           ],
           "status" : "OK"
        }

如果你有谷歌地图API密钥,那么你可以添加参数result_type来指定你感兴趣的地址类型,我想也许street_address