【问题标题】:Get country from coordinates?从坐标获取国家?
【发布时间】:2012-06-20 09:54:41
【问题描述】:

如何在Android中获取国家名称,如果已知地理坐标?怎么做最简单?

【问题讨论】:

标签: android google-maps geolocation gps


【解决方案1】:

给你

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);

    if (addresses.size() > 0)
     {  
       String countryName=addresses.get(0).getCountryName();
     }

【讨论】:

    【解决方案2】:

    为此使用下面的函数。

    public void getAddress(double lat, double lng) {
        Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
            Address obj = addresses.get(0);
            String add = obj.getAddressLine(0);
            GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                    + obj.getAdminArea();
            GUIStatics.latitude = obj.getLatitude();
            GUIStatics.longitude = obj.getLongitude();
            GUIStatics.currentCity= obj.getSubAdminArea();
            GUIStatics.currentState= obj.getAdminArea();
            add = add + "\n" + obj.getCountryName();
            add = add + "\n" + obj.getCountryCode();
            add = add + "\n" + obj.getAdminArea();
            add = add + "\n" + obj.getPostalCode();
            add = add + "\n" + obj.getSubAdminArea();
            add = add + "\n" + obj.getLocality();
            add = add + "\n" + obj.getSubThoroughfare();
    
            Log.v("IGA", "Address" + add);
            // Toast.makeText(this, "Address=>" + add,
            // Toast.LENGTH_SHORT).show();
    
            // TennisAppActivity.showDialog(add);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    

    在清单中添加以下权限

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    

    【讨论】:

    • 您的代码有错误。有时,从latlng,你没有地址。这意味着address.get(0) 引发IndexOutOfBound 异常。
    • 我会以更快的方式获取国家/地区代码(我不需要完整地址),但这里 Geocoder 工作缓慢
    【解决方案3】:

    使用这个

    try {
            Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
            List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            if (addresses.isEmpty()) {
                placeName.setText("Waiting for Location");
            }
            else {
                if (addresses.size() > 0) {
                    placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
                }
            }
        }
        catch(Exception e){
            Toast.makeText(this, "No Location Name Found", 600).show();
        }
    

    在清单文件中使用它

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    

    【讨论】:

      【解决方案4】:

      我最近的做法是从 Web API URL 读取数据并解析 JSON。

      点 (40.714224, -73.961452) 的示例 URL 是:

      http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key
      

      产生以下输出:

      {
        "name": "40.714224,-73.961452",
        "Status": {
          "code": 200,
          "request": "geocode"
        },
        "Placemark": [ {
          "id": "p1",
          "address": "285 Bedford Ave, Brooklyn, NY 11211, USA",
          "AddressDetails": {
         "Accuracy" : 8,
         "Country" : {
            "AdministrativeArea" : {
               "AdministrativeAreaName" : "NY",
               "SubAdministrativeArea" : {
                  "Locality" : {
                     "DependentLocality" : {
                        "DependentLocalityName" : "Williamsburg",
                        "PostalCode" : {
                           "PostalCodeNumber" : "11211"
                        },
                        "Thoroughfare" : {
                           "ThoroughfareName" : "285 Bedford Ave"
                        }
                     },
                     "LocalityName" : "Brooklyn"
                  },
                  "SubAdministrativeAreaName" : "Kings"
               }
            },
            "CountryName" : "USA",
            "CountryNameCode" : "US"
         }
      },
          "ExtendedData": {
            "LatLonBox": {
              "north": 40.7154779,
              "south": 40.7127799,
              "east": -73.9600584,
              "west": -73.9627564
            }
          },
          "Point": {
            "coordinates": [ -73.9614074, 40.7141289, 0 ]
          }
        } ]
      }
      

      我发现 GSON 非常适合在 Android 中解析 JSON。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-11
        相关资源
        最近更新 更多