java - 位置不更新
问题描述
我有这段代码,它使用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());
}
推荐阅读
- c++ - 为什么我不能在 `while` 循环中验证 `pow` 函数?
- r - 使用 ggplot 将两个矩阵合二为一
- php - 无法访问本地主机中的数据库,即使它确实存在
- r-markdown - 我可以在 RMarkdown 中引用使用 table1 制作的表格吗?
- python - 寻找关键词
- perl - -s 是否总是在 Perl 中的目录上返回零?
- java - 如何在 Java 中使用 ArrayList 删除不等于对象
- monaco-editor - 如何在自定义令牌提供程序中启用多行匹配
- python - 参数 1 必须是类型,而不是 super() 调用上的 pygame.surface
- ruby - 一次初始化多个对象?