英文:
How to get the onLocationResult be triggered all the 30m of displacement in a LocationRequest with the locationCallback?
问题
我不理解,毫秒间隔有效,但最小距离不起作用。如何使onLocationResult在LocationRequest与locationCallback中的30米位移触发?
if (locationRequest == null) {
locationRequest = new LocationRequest();
if (drivingtype == Constants.DRIVING_TYPE_BICYCLE) {
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION);
locationRequest.setFastestInterval(Constants.GPS_UPDATE_PRECISION);
locationRequest.setSmallestDisplacement(30f);
} else {
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION);
locationRequest.setFastestInterval((Constants.GPS_UPDATE_PRECISION) / 4);
locationRequest.setSmallestDisplacement(30f);
}
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
}
if (locationCallback == null) {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// 处理位置更新
// 位置已更改。获取坐标并更新地图。
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
};
}
英文:
I don't understand, the Interval in milliseconds works well, but the minimum distance doesn't work. How to get the onLocationResult be triggered all the 30m of displacement in a LocationRequest with the locationCallback ?
if (locationRequest == null) {
locationRequest = new LocationRequest();
if (drivingtype == Constants.DRIVING_TYPE_BICYCLE) {
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION);
locationRequest.setFastestInterval(Constants.GPS_UPDATE_PRECISION);
locationRequest.setSmallestDisplacement(30f);
} else {
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION);
locationRequest.setFastestInterval((Constants.GPS_UPDATE_PRECISION) / 4);
locationRequest.setSmallestDisplacement(30f);
}
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER );
}
if (locationCallback == null) {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// Traitement des mises à jour de localisation
// La position a changé. Obtenez les coordonnées et mettez à jour la carte.
latitude = location.getLatitude();
longitude = location.getLongitude();
答案1
得分: 0
根据@Jemshit提供的答案,以下是代码部分的翻译:
// 时间
如果(locationRequest == null){
locationRequest = LocationRequest.create();
如果(drivingtype == Constants.DRIVING_TYPE_BICYCLE){
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// locationRequest.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION); // 每分钟更新一次位置
locationRequest.setFastestInterval(10000);
} else {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// locationRequest.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION / 4);
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION); // 每分钟更新一次位置
locationRequest.setFastestInterval(10000);
}
}
如果(locationCallback == null){
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// 处理位置更新
// 位置已更改。获取坐标并更新地图。
latitude = location.getLatitude();
longitude = location.getLongitude();
map.getOverlays().clear();
// 使用新位置更新地图。
// 例如,使用OsmAnd的功能来移动摄像头或添加标记。
GeoPoint currentPosition = new GeoPoint(latitude, longitude);
//moveMapCameraToPosition(currentPosition); // 将摄像头移动到新位置
animateMapCameraToPosition(currentPosition);
map.getOverlays().remove(accuracyOverlay);
int greenColorWithOpacity = Color.argb(128, 0, 255, 0);
accuracyOverlay = new AccuracyOverlay(new GeoPoint(location), Constants.ACCURACYOVERLAYPRECISION, greenColorWithOpacity);
map.getOverlayManager().getTilesOverlay().setLoadingBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
map.getOverlays().add(accuracyOverlay);
map.invalidate();
// 您还可以在当前位置添加标记
addMarkerToPosition(currentPosition);
// 将位置发送到API
sendPositionToAPI(latitude, longitude);
}
}
};
}
// 距离
如果(locationRequest2 == null){
locationRequest2 = new LocationRequest();
如果(drivingtype == Constants.DRIVING_TYPE_BICYCLE){
locationRequest2.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
} else {
locationRequest2.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
}
locationRequest2.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY );
}
如果(locationCallback2 == null){
locationCallback2 = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// 处理位置更新
// 位置已更改。获取坐标并更新地图。
latitude = location.getLatitude();
longitude = location.getLongitude();
map.getOverlays().clear();
// 使用新位置更新地图。
// 例如,使用OsmAnd的功能来移动摄像头或添加标记。
GeoPoint currentPosition = new GeoPoint(latitude, longitude);
//moveMapCameraToPosition(currentPosition); // 将摄像头移动到新位置
animateMapCameraToPosition(currentPosition);
map.getOverlays().remove(accuracyOverlay);
int greenColorWithOpacity = Color.argb(128, 0, 255, 0);
accuracyOverlay = new AccuracyOverlay(new GeoPoint(location), Constants.ACCURACYOVERLAYPRECISION, greenColorWithOpacity);
map.getOverlayManager().getTilesOverlay().setLoadingBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
map.getOverlays().add(accuracyOverlay);
map.invalidate();
// 您还可以在当前位置添加标记
addMarkerToPosition(currentPosition);
// 将位置发送到API
sendPositionToAPI(latitude, longitude);
}
}
};
}
Looper mainLooper = Looper.getMainLooper();
Looper mainLooper2 = Looper.getMainLooper();
如果(!locationUpdatesEnabled){
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, mainLooper);
fusedLocationClient2.requestLocationUpdates(locationRequest2, locationCallback2, mainLooper2);
locationUpdatesEnabled = true;
}
英文:
SOLUTION:
Based on the answer proponed by @Jemshit,
//Time
if (locationRequest == null) {
locationRequest = LocationRequest.create();
if (drivingtype == Constants.DRIVING_TYPE_BICYCLE) {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// locationRequest.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION); // Update location every 1 minute
locationRequest.setFastestInterval(10000);
} else {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// locationRequest.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION / 4);
locationRequest.setInterval(Constants.GPS_UPDATE_TIMING_PRECISION); // Update location every 1 minute
locationRequest.setFastestInterval(10000);
}
}
if (locationCallback == null) {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// Traitement des mises à jour de localisation
// La position a changé. Obtenez les coordonnées et mettez à jour la carte.
latitude = location.getLatitude();
longitude = location.getLongitude();
map.getOverlays().clear();
// Mettez à jour la carte avec la nouvelle position.
// Par exemple, utilisez les fonctions de OsmAnd pour déplacer la caméra ou ajouter un marqueur.
GeoPoint currentPosition = new GeoPoint(latitude, longitude);
//moveMapCameraToPosition(currentPosition); // Déplace la caméra vers la nouvelle position
animateMapCameraToPosition(currentPosition);
map.getOverlays().remove(accuracyOverlay);
int greenColorWithOpacity = Color.argb(128, 0, 255, 0);
accuracyOverlay = new AccuracyOverlay(new GeoPoint(location), Constants.ACCURACYOVERLAYPRECISION, greenColorWithOpacity);
map.getOverlayManager().getTilesOverlay().setLoadingBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
map.getOverlays().add(accuracyOverlay);
map.invalidate();
// Vous pouvez également ajouter un marqueur à la position actuelle
addMarkerToPosition(currentPosition);
// Envoi de la position à l'API
sendPositionToAPI(latitude, longitude);
}
}
};
}
//Distance
if (locationRequest2 == null) {
locationRequest2 = new LocationRequest();
if (drivingtype == Constants.DRIVING_TYPE_BICYCLE) {
locationRequest2.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
} else {
locationRequest2.setSmallestDisplacement(Constants.GPS_UPDATE_PRECISION);
}
locationRequest2.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY );
}
if (locationCallback2 == null) {
locationCallback2 = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
// Traitement des mises à jour de localisation
// La position a changé. Obtenez les coordonnées et mettez à jour la carte.
latitude = location.getLatitude();
longitude = location.getLongitude();
map.getOverlays().clear();
// Mettez à jour la carte avec la nouvelle position.
// Par exemple, utilisez les fonctions de OsmAnd pour déplacer la caméra ou ajouter un marqueur.
GeoPoint currentPosition = new GeoPoint(latitude, longitude);
//moveMapCameraToPosition(currentPosition); // Déplace la caméra vers la nouvelle position
animateMapCameraToPosition(currentPosition);
map.getOverlays().remove(accuracyOverlay);
int greenColorWithOpacity = Color.argb(128, 0, 255, 0);
accuracyOverlay = new AccuracyOverlay(new GeoPoint(location), Constants.ACCURACYOVERLAYPRECISION, greenColorWithOpacity);
map.getOverlayManager().getTilesOverlay().setLoadingBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
map.getOverlays().add(accuracyOverlay);
map.invalidate();
// Vous pouvez également ajouter un marqueur à la position actuelle
addMarkerToPosition(currentPosition);
// Envoi de la position à l'API
sendPositionToAPI(latitude, longitude);
}
}
};
}
Looper mainLooper = Looper.getMainLooper();
Looper mainLooper2 = Looper.getMainLooper();
if (!locationUpdatesEnabled) {
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, mainLooper);
fusedLocationClient2.requestLocationUpdates(locationRequest2, locationCallback2, mainLooper2);
locationUpdatesEnabled = true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论