如何查找特定位置的经度和纬度?
用户在edittext中输入位置并单击搜索按钮,然后该位置将显示在googlemap中 .
我使用下面的代码,但这是错误的“服务不可用”
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
address=geoCoder.getFromLocationName(txtlocation.getText().toString(), 1).get(0);
double longi=address.getLongitude();
double latit=address.getLatitude();
System.out.println("longitude:--- " +longi);
System.out.println("latitude:---" +latit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MapRouteActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
2 回答
试着用
对getLastKnownLocation()的调用不会阻塞 - 这意味着如果当前没有位置,它将返回null - 所以你可能想看看将LocationListener传递给requestLocationUpdates()方法,这将为你提供异步更新你所在的位置 .
如果要使用GPS,则需要为应用程序提供ACCESS_FINE_LOCATION权限 .
您可能还想在GPS不可用时添加ACCESS_COARSE_LOCATION权限,并使用getBestProvider()方法选择您的位置提供程序 .
试试这段代码