即使在这里有很多阅读,我也想不出这个问题。看来我从来没有打开过GPS事件。
经过一些调试检查后,标志isGps是正确的。但是,结果是,用户警报效果很好,但无法更新位置。我只想在打开gps时添加一个标记,看来这里的内容未正确同步。
即使位置准确度很高。
我使用片段中的代码。
public class MapDialogFragment extends DialogFragment
implements OnMapReadyCallback, GoogleMap.OnMarkerDragListener {
.....
private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
boolean isGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPS){
// never goes here WHEN GPS TURNED ON !!! WHY ?? MAKES ME CRAZY
// + how get current location here ? is updateMyLocation call ok ?
updateMyLocation();
}
else{
// user alert
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.message_location_error_gps_off), Toast.LENGTH_LONG).show();
}
}
}
};
@Override
public void onResume() {
super.onResume();
getContext().registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
}
@Override
public void onPause() {
super.onPause();
getContext().unregisterReceiver(mGpsSwitchStateReceiver);
}
private void updateMyLocation(){
Task locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful() && task.getResult() != null) {
// Set the map's camera position to the current location of the device.
mLastKnownLocation = (Location) task.getResult();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
latitude = mLastKnownLocation.getLatitude();
longitude = mLastKnownLocation.getLongitude();
// add marker
buildMarkerFromLocation();
} else {
Log.d(TAG, "Current location is null. Using defaults.");
Log.e(TAG, "Exception: %s", task.getException());
}
}
});
}
...
}
所以这是我的逻辑:
因此,基本上,如何在没有硬编码的情况下提供默认位置?
这里是有关Google api页面的更多信息:
getLastLocation()方法返回一个Task,您可以使用该Task获取具有地理位置的纬度和经度坐标的Location对象。在以下情况下,定位对象可能为null:
在设备设置中位置已关闭。即使先前已检索到最后一个位置,结果也可能为null,因为禁用位置还会清除缓存。
该设备从未记录其位置,可能是新设备或已恢复为出厂设置的设备。
设备上的Google Play服务已重新启动,并且在服务重新启动后,没有活动的融合位置提供程序客户端请求位置。为了避免这种情况,您可以创建一个新的客户端并自己请求位置更新。有关更多信息,请参阅接收位置更新。
希望我能在您的帮助下解决此问题。应该是显而易见的东西,但我不知道出了什么问题...
因为您收到事件,这意味着它与设备本身无关,并且看起来像在您的更新位置,在我的情况下,我使用的是'OnSuccessListener'而不是'OnCompleteListener',我不知道是否会成为问题,但是无论如何,这就是我如何更新自己的位置:
/**
* To get the current MapLocation and add marker at that location
*/
@SuppressLint("MissingPermission")
private void setCurrentLocation() {
try {
FusedLocationProviderClient locationClient = new FusedLocationProviderClient(getActivity());
if (checkLocationPermissions()) {
Task<Location> currentLocationTask = locationClient.getLastLocation(); // already checked
currentLocationTask.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
try {
// Now, add the required Marker
addMarker(new LatLng(location.getLatitude(), location.getLongitude()));
} catch (NullPointerException npe) {
npe.printStackTrace();
Toast.makeText(getActivity(), getResources().getString(R.string.cannot_get_location), Toast.LENGTH_SHORT).show();
}
}
});
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Adding a Marker at the Map
*
* @param point the point contains the latitude and longitude to add the marker at
*/
private void addMarker(LatLng point) {
try {
marker.remove();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
MarkerOptions markerOptions = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title(getAddressAtLocation(point));
marker = mMap.addMarker(markerOptions);
currentChosenLocation = point;
}