【发布时间】:2012-05-10 16:28:17
【问题描述】:
我必须等待才能获得当前位置,就像您在 Google 地图中并请求您的位置时一样。如果它无法获得任何位置,则通知错误并且什么也不做。
我在这里和网上阅读了答案,但都说我必须使用getLastKnownLocation 方法,但我不需要这个,我只需要当前位置。如果没有,我什么都不做。
【问题讨论】:
我必须等待才能获得当前位置,就像您在 Google 地图中并请求您的位置时一样。如果它无法获得任何位置,则通知错误并且什么也不做。
我在这里和网上阅读了答案,但都说我必须使用getLastKnownLocation 方法,但我不需要这个,我只需要当前位置。如果没有,我什么都不做。
【问题讨论】:
从昨天开始阅读:GPS Android - get positioning only once
但是,您可以通过以下方式获取当前位置:
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) {}
}
【讨论】:
db.insert(location.getTime(), location.getLatitude(), location.getLongitude()); 之类的方法将位置保存到数据库中。数据库方面的帮助start here