在android中是否有任何替代的Address类的getLocality()方法

时间:2014-12-10 12:42:14

标签: java android

getLocality()方法为某些坐标返回null值。或者,如果有任何其他api获取地址,请告诉我.... 提前致谢

1 个答案:

答案 0 :(得分:0)

尝试使用此代码...

List<Address> addresses;

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//get the last known latitude longitude from network
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//create an instance of geocoder
geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());

if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
    // Build the alert dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(Html.fromHtml("<font color='#000000'>Location Service Not Active</font>"));

    builder.setMessage("Please Enable Location Services ");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
       {
        public void onClick(DialogInterface dialogInterface, int i) {
            // Show location settings when the user acknowledges the alert dialog
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });
    Dialog alertDialog = builder.create();
    alertDialog.setCanceledOnTouchOutside(false);
    alertDialog.show();

}

else
{
    addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
    if(addresses.get(0).getPostalCode() != null)
    {
        String currentZip = addresses.get(0).getPostalCode();
        // currentZip which get gip code
    }
    else
    {
        // current zip not found
    }
}