gpt4 book ai didi

java - 位置不更新

转载 作者:行者123 更新时间:2023-12-02 01:59:11 24 4
gpt4 key购买 nike

我有这段代码,它通过 onLocationChanged 获取当前位置。我的问题是当我搬家时。我希望改变它的位置。我得到的是该应用程序找到了我当前的位置。但当我移动时它不会更新位置。它仍然获得第一位置。我认为我的权限有问题。你能帮我找出代码中的错误,以便每次移动时位置都会更新吗?谢谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_create_new_path);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkUserLocationPermission();
}

// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

ButtonStart = findViewById(R.id.ButtonStart);
ButtonEnd = findViewById(R.id.ButtonEnd);

if (count == 0) {
ButtonEnd.setEnabled(false);
ButtonStart.setEnabled(true);
}

ButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
next_page(v);
}
});
ButtonEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
next_page(v);
}
});

points = new ArrayList<>();
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

buildGoogleApiClient();

mMap.setMyLocationEnabled(true);
}
}

public boolean checkUserLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code);
}
return false;
} else {
return true;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case Request_User_Location_Code:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (googleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
} else {
Toast.makeText(this, "Permission Denied...", Toast.LENGTH_SHORT).show();
}
}
}

protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

googleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {

if (currentUserLocationMarker != null) {
currentUserLocationMarker.remove();
}

latLng = new LatLng(location.getLatitude(), location.getLongitude());

MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("user Current Location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));

currentUserLocationMarker = mMap.addMarker(markerOptions);

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(14));

if (googleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}

points.add(latLng); //added

redrawLine(); //added
}

@Override
public void onConnected(@Nullable Bundle bundle) {

locationRequest = new LocationRequest();
locationRequest.setInterval(INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
locationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
}

最佳答案

你可以这样尝试:-

//variable
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
private Location mCurrentLocation;
private GoogleMap mGoogleMap;

//for current mLocation continue (declare this after declare variable)
LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult.getLastLocation() != null) {
mCurrentLocation = locationResult.getLastLocation();
Log.d(TAG, "onLocationResult: "+mCurrentLocation);
currentLocation();
}
}
};

//call after take permission of location (or in onMapReady())
//Manifest.permission.ACCESS_COARSE_LOCATION,
//Manifest.permission.ACCESS_FINE_LOCATION
private void getlocation(){
//send request for location add this method for get call back from time interval-->// .setInterval(2000).setFastestInterval(2000)
mLocationRequest = new LocationRequest().setInterval(2000).setFastestInterval(2000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
//send current location request
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}

关于java - 位置不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51884876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com