从坐标获取国家?

时间:2023-03-22
本文介绍了从坐标获取国家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

How to get country name in Android, If are known geo coordinates? How to make it the simplest way?

推荐答案

为此使用下面的函数.

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 + "
" + obj.getCountryName();
        add = add + "
" + obj.getCountryCode();
        add = add + "
" + obj.getAdminArea();
        add = add + "
" + obj.getPostalCode();
        add = add + "
" + obj.getSubAdminArea();
        add = add + "
" + obj.getLocality();
        add = add + "
" + 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"/>

这篇关于从坐标获取国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Phonegap 地理定位有时无法在 android 上运行 下一篇:Android:使用 LocationManager.requestLocationUpdates() 时如何从 Int

相关文章