等待当前位置 - GPS - Android Dev [英] Wait for current location - GPS - Android Dev
问题描述
我必须等待才能获取当前位置,就像您在Google地图中一样,并且它会请求您的位置。如果它不能得到任何位置通知错误,什么都不做。
我在这里和web上看到了答案,但都说我必须使用 getLastKnownLocation
方法,但我不会我不需要这个,我只需要当前的位置。如果没有,我什么都不做。 解决方案
从昨天开始阅读: 但是,您可以通过以下方式获取当前位置: I have to wait to get the current location, just like when you are in Google maps and it requests for your location. If it can't get any location inform the error and do nothing. I read the answers here and in web but all say that I have to use Read this from yesterday: GPS Android - get positioning only once But here is how you can get the current location:
这篇关于等待当前位置 - GPS - Android Dev的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
public class Activity实现LocationListener {
LocationManager mLocationManager;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
位置位置= mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!= null&& location.getTime()> Calendar.getInstance()。getTimeInMillis() - 2 * 60 * 1000){
//对最近的位置做些什么修正
//如果它小于两分钟旧,
//否则等待更新低于
}
}
public void onLocationChanged(位置位置){
if(location!= null){
Log.v(Location Changed,location.getLatitude()+and+ location.getLongitude());
//您需要在完成后调用它:
// mLocationManager.removeUpdates(this);
//所需函数
public void onProviderDisabled(String arg0){}
public void onProviderEnabled(String arg0){}
public void onStatusChanged(String arg0,int arg1,Bundle arg2){}
}
getLastKnownLocation
method, but I don't need this, I just need the current location. If not, I don't do anything.public class Example extends Activity implements LocationListener {
LocationManager mLocationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
// Do something with the recent location fix
// if it is less than two minutes old,
// otherwise wait for the update below
}
}
public void onLocationChanged(Location location) {
if (location != null) {
Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
// You need to call this whenever you are done:
// mLocationManager.removeUpdates(this);
}
}
// Required functions
public void onProviderDisabled(String arg0) {}
public void onProviderEnabled(String arg0) {}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}