我的当前位置不正确,标记指示的位置在150米左右,此问题会影响应用程序,因为它在位置方面很敏感
这是我的Java代码,可在我的应用中使用的地图上显示我的当前位置:
public class HomeFragment extends Fragment implements OnMapReadyCallback {
private HomeViewModel homeViewModel;
private GoogleMap mMap;
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
SettingsClient mSettingsClient;
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_maps, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
mSettingsClient = LocationServices.getSettingsClient(getActivity());
fetchLastLocation();
}
private void fetchLastLocation() {
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(HomeFragment.this);
}
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().isMyLocationButtonEnabled();
mMap.getUiSettings().isZoomControlsEnabled();
mMap.getUiSettings().setMyLocationButtonEnabled(true);
// Add a marker in Sydney and move the camera
LatLng myLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLatLng));
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(myLatLng).zoom(15.5f).build()));
}
}
我不确定只是尝试进行此更改:-
mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location").anchor(0.5f, 0.5f));