无法从lat,long值获得城市?

时间:2011-05-19 09:19:14

标签: android

Geocoder g = new Geocoder(this, Locale.getDefault());
    java.util.List<android.location.Address> result = null;

    // testing

    try{
        result = g.getFromLocation(43.324722,21.903333, 1);
        if (result.size() > 0) {
            selectedCity = result.get(0).getLocality();
        }else
            //no city found
        }catch(Exception ex)
        {

        }

我使用上面的代码来获取Android中的城市价值,但它总是显示没有找到任何一个城市指导我可能是什么问题?

1 个答案:

答案 0 :(得分:1)

public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(this, 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();
    }
}

我正在使用此功能获取与纬度和经度相关的所有信息,只需通过此功能中的纬度和经度即可找到答案。 我希望这对你很有帮助。