Google 地图 Android API 只记录一次数据
Google Maps Android API logging data only once
我正在尝试使用 google maps api v2 来记录用户的位置并将其存储到 SQLite 数据库中。我设法做到了这一点。但是现在问题出现了,因为我的位置数据只被记录了一次。一旦用户按下一个按钮,该位置应该会持续记录,直到用户按下另一个按钮。但是,就我而言;该应用程序会在用户按下按钮时记录位置,仅此而已。该位置仅记录一次。我曾尝试使用 mLocationRequest 句柄来操纵时间,但更改时间没有任何效果;我的数据只被记录一次。
有人可以帮帮我吗?
private void handleNewLocation(Location location) {
String key = "xx";
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
Log.d("location", String.valueOf(latLng));
databaseHandler.enterMapValues( _workOutId,String.valueOf(latLng));
// Log.d("location new ", String.valueOf(latLng));
old_longitude = currentLongitude;
old_latitude = currentLatitude;
}
此函数用于获取新位置。我在 onConnected 中调用它:
public void onConnected(Bundle bundle) {
Log.i(TAG, "location services connected");
location_current = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); //to get last location
if (location_current == null) { //if last location is null, only then it will request new location
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location_current);
}
}
我删除了 onConnected 中的 if-else。现在它每 5 秒获取一次位置,因为那是在 onCreate() 的 mLocationRequest 中指定的时间。这解决了我的问题。
我正在尝试使用 google maps api v2 来记录用户的位置并将其存储到 SQLite 数据库中。我设法做到了这一点。但是现在问题出现了,因为我的位置数据只被记录了一次。一旦用户按下一个按钮,该位置应该会持续记录,直到用户按下另一个按钮。但是,就我而言;该应用程序会在用户按下按钮时记录位置,仅此而已。该位置仅记录一次。我曾尝试使用 mLocationRequest 句柄来操纵时间,但更改时间没有任何效果;我的数据只被记录一次。 有人可以帮帮我吗?
private void handleNewLocation(Location location) {
String key = "xx";
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
Log.d("location", String.valueOf(latLng));
databaseHandler.enterMapValues( _workOutId,String.valueOf(latLng));
// Log.d("location new ", String.valueOf(latLng));
old_longitude = currentLongitude;
old_latitude = currentLatitude;
}
此函数用于获取新位置。我在 onConnected 中调用它:
public void onConnected(Bundle bundle) {
Log.i(TAG, "location services connected");
location_current = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); //to get last location
if (location_current == null) { //if last location is null, only then it will request new location
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location_current);
}
}
我删除了 onConnected 中的 if-else。现在它每 5 秒获取一次位置,因为那是在 onCreate() 的 mLocationRequest 中指定的时间。这解决了我的问题。