开发者

Unable to get city from lat,long values?

开发者 https://www.devze.com 2023-03-07 11:39 出处:网络
Geocoder g = new Geocoder(this, Locale.getDefault()); java.util.List<android.location.Address> result = null;
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)
        {

        }

i am using above code to get city value in android but it always show no city found any one guide me what c开发者_JAVA技巧ould be the problem?


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

I am using this function for getting all the information related to latitude and longitude just pass the latitude and longitude in this function then you find your answer. I hope this is very help full to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号