在java中,通过谷歌获取地址的经度和纬度的最佳方式是什么

What is the best way to get longitude and latitude for an address via Google in java

本文关键字:经度 纬度 最佳 是什么 方式 地址 获取 java 谷歌      更新时间:2023-09-26

我正在尝试编写一些从地址获取位置的java代码。(不在android环境中。)

如果可能的话,我希望它是永久的方式,只要谷歌以某种方式提供地理定位服务。

我想我不能说谷歌javascript地理定位api使用永久URL来实现这一点,因为api的Object在运行时来自谷歌服务器。

然而,我认为安卓可能会使用一个永久的URL来实现这一点,因为谷歌无法更改设备获得地理定位服务的每个设备的URL。

我错了吗?谷歌是否有相关政策?

提前谢谢。

您可以尝试使用Google HTTP API来获取地理编码和反向地理编码。

地理编码请求

地理编码API请求必须采用以下形式:

http://maps.googleapis.com/maps/api/geocode/output?parameters

其中输出可以是以下值之一:

json(推荐)表示JavaScript对象表示法(json)中的输出xml表示输出为xml

关注此链接了解更多信息:https://developers.google.com/maps/documentation/geocoding/

如果你想使用谷歌地理编码,那么这些东西对你很有用:

地理编码API请求必须采用以下形式:

http://maps.googleapis.com/maps/api/geocode/output?parameters

其中输出可以是以下值之一:

json (recommended) indicates output in JavaScript Object Notation (JSON)
xml indicates output as XML

要通过HTTPS访问Geocoding API,请使用:

https://maps.googleapis.com/maps/api/geocode/output?parameters

对于在请求中包含敏感用户数据(如用户位置)的应用程序,建议使用HTTPS。

在任何一种情况下,都需要某些参数,而有些参数是可选的。按照URL中的标准,所有参数都使用"与"(&)字符分隔。下面列举了参数列表及其可能的值。

所需参数

address — The address that you want to geocode.
     or
latlng — The textual latitude/longitude value for which you wish to obtain the closest, human-readable address. See Reverse Geocoding for more information.
     or
components — A component filter for which you wish to obtain a geocode. See Component Filtering for more information. The components filter will also be accepted as an optional parameter if an address is provided.
sensor — Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be either true or false.

用于业务用户的映射API必须在其地理编码请求中包含有效的客户端和签名参数。有关详细信息,请参阅用于Business Web Services的Maps API。

可选参数

bounds — The bounding box of the viewport within which to bias geocode results more prominently. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Viewport Biasing below.)
language — The language in which to return results. See the list of supported domain languages. Note that we often update supported languages so this list may not be exhaustive. If language is not supplied, the geocoder will attempt to use the native language of the domain from which the request is sent wherever possible.
region — The region code, specified as a ccTLD ("top-level domain") two-character value. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Region Biasing below.)
components — The component filters, separated by a pipe (|). Each component filter consists of a component:value pair and will fully restrict the results from the geocoder. For more information see Component Filtering, below.

JSON输出格式

在本例中,Geocoding API请求对"1600 Amphitheatre Parkway,Mountain View,CA"的查询做出json响应:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+露天剧场+公园大道,+山+景,+CA&传感器=真_假

在本例中,我们将传感器参数保留为变量true_or_false,以强调必须将此值显式设置为true或false。

此请求返回的JSON如下所示。请注意,实际的JSON可能包含较少的空白。您不应该对请求之间的空白数量或格式做出假设。

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.42291810,
               "lng" : -122.08542120
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42426708029149,
                  "lng" : -122.0840722197085
               },
               "southwest" : {
                  "lat" : 37.42156911970850,
                  "lng" : -122.0867701802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

以下是一些示例代码,可以帮助您获取纬度和经度:

public static void main(String[] args) {
        try 
        {
            URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
            URLConnection conn = url.openConnection();                                                                    
            conn.connect();
            InputStreamReader isr = new InputStreamReader(conn.getInputStream());
            StringBuffer sbLocation = new StringBuffer();
            for (int i=0; i != -1; i = isr.read())
            {   
                sbLocation.append((char)i);
            }
            String getContent = sbLocation.toString().trim();   
            if(getContent.contains("results"))
            {
                String temp = getContent.substring(getContent.indexOf("["));
                JSONArray JSONArrayForAll = new JSONArray(temp);
                String lng = JSONArrayForAll.getJSONObject(0).getJSONObject("geometry").getJSONObject("location").get("lng").toString();
                String lat = JSONArrayForAll.getJSONObject(0).getJSONObject("geometry").getJSONObject("location").get("lat").toString();
                System.out.println(" Latitude : " + lat);
                System.out.println(" Longitude : " + lng);
            }
        }
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
}