代码之家  ›  专栏  ›  技术社区  ›  Hitrene

用GPS坐标确定一个国家

  •  0
  • Hitrene  · 技术社区  · 7 年前

    我有一些由LocationManager提供动力的GPS坐标。当设备连接到internet时,我必须检查我去过哪个国家。是否有任何服务允许您发送GPS位置并接收国家/地区的响应?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Abhishek D    7 年前

    您可以使用它来获取存储为给定经度和纬度中的地址的所有详细信息。

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(new Criteria(), true);
    
    Location locations = locationManager.getLastKnownLocation(provider);
    List<String>  providerList = locationManager.getAllProviders();
    if(null!=locations && null!=providerList && providerList.size()>0){                 
    double longitude = locations.getLongitude();
    double latitude = locations.getLatitude();
    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());                 
    try {
        List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
        if(null!=listAddresses&&listAddresses.size()>0){
            String _Location = listAddresses.get(0).getAddressLine(0);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    }
    

    如果你需要更多经纬度的详细信息,你可以,

    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();
        }
    }
    

    不要忘记在清单文件中添加权限。

        2
  •  1
  •   Deven    7 年前

    使用地理编码器,您可以获得如下国家:

    private String getCountry(double latitude, double longitude) {
            try {
                List<Address> addresses;
                Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
                addresses = geocoder.getFromLocation(latitude, longitude, 1); 
                return addresses.get(0).getCountryName();;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return  "Unknown address";
        }