Java android中Address类的getLocality()方法是否有其他选择
getLocality()方法正在为某些坐标返回null值。或者,如果有任何其他api获取地址,请让我知道。。。。Java android中Address类的getLocality()方法是否有其他选择,java,android,Java,Android,getLocality()方法正在为某些坐标返回null值。或者,如果有任何其他api获取地址,请让我知道。。。。 提前感谢尝试使用此代码… List<Address> addresses; LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //get the last known latitude longitude from network Location lo
提前感谢尝试使用此代码…
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
}
}
列出地址;
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
//从网络中获取最后一个已知的经纬度
Location Location=lm.getLastKnownLocation(LocationManager.NETWORK\u提供程序);
//创建geocoder的实例
geo=新的地理编码器(this.getApplicationContext(),Locale.getDefault());
如果(!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
//构建警报对话框
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(Html.fromHtml(“位置服务未激活”);
builder.setMessage(“请启用位置服务”);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener()
{
公共void onClick(DialogInterface,inti){
//当用户确认警报对话框时显示位置设置
意向意向=新意向(设置、动作、位置、来源、设置);
星触觉(意向);
}
});
Dialog alertDialog=builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
其他的
{
地址=geo.getFromLocation(location.getLatitude(),location.getLatitude(),1);
if(addresses.get(0).getPostalCode()!=null)
{
字符串currentZip=addresses.get(0.getPostalCode();
//获取gip代码的currentZip
}
其他的
{
//找不到当前的zip文件
}
}
嗨,有人能帮我解决网络提供商的问题吗。。为了获取位置…嗨,阿弥陀佛,我想通过纬度和经度来确定位置,这是我从其他来源获得的,而不是从GPS获得的。下面为您提供示例代码。尝试一下并给出您的反馈@mhsn感谢您对阿弥陀佛的支持。我只有一个选择来获取城市,即通过纬度和经度来搜索城市。但现在不需要城市。因此,相应地更改代码。