Java 位置不更新
我有这个代码,它通过Java 位置不更新,java,android,currentlocation,on-location-changed,Java,Android,Currentlocation,On Location Changed,我有这个代码,它通过onLocationChanged获取当前位置。我的问题是当我搬家的时候。我想改变它的位置。我得到的是应用程序找到了我的当前位置。但它不会在我移动时更新位置。它仍然获得第一个位置。我想我的权限有问题。你能帮我找出代码中的错误吗?这样每次我搬家时,位置都会更新?多谢各位 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
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);
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u maps\u create\u new\u path);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
checkUserLocationPermission();
}
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
ButtonStart=findViewById(R.id.ButtonStart);
ButtonEnd=findViewById(R.id.ButtonEnd);
如果(计数=0){
按钮和设置启用(错误);
ButtonStart.setEnabled(true);
}
ButtonStart.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
下一页(五);
}
});
Buttonnend.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
下一页(五);
}
});
points=新的ArrayList();
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
buildGoogleAppClient();
mMap.setMyLocationEnabled(真);
}
}
公共布尔值checkUserLocationPermission(){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予){
if(ActivityCompat.shouldshowRequestPermissionRegulation(这是Manifest.permission.ACCESS\u FINE\u位置)){
ActivityCompat.requestPermissions(这是一个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},Request\u User\u LOCATION\u Code);
}否则{
ActivityCompat.requestPermissions(这是一个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},Request\u User\u LOCATION\u Code);
}
返回false;
}否则{
返回true;
}
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
开关(请求代码){
案例请求\用户\位置\代码:
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
if(googleApiClient==null){
buildGoogleAppClient();
}
mMap.setMyLocationEnabled(真);
}
}否则{
Toast.makeText(此“权限被拒绝…”,Toast.LENGTH_SHORT.show();
}
}
}
受保护的同步无效BuildGoogleAppClient(){
GoogleapClient=新的GoogleapClient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
@凌驾
已更改位置上的公共无效(位置){
if(currentUserLocationMarker!=null){
currentUserLocationMarker.remove();
}
latLng=新latLng(location.getLatitude(),location.getLongitude());
MarkerOptions MarkerOptions=新MarkerOptions();
标记选项位置(板条);
markerOptions.title(“用户当前位置”);
图标(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
currentUserLocationMarker=mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(14));
if(googleApiClient!=null){
LocationServices.FusedLocationApi.RemovelocationUpdate(GoogleAppClient,this);
}
points.add(latLng);//添加
重画线();//已添加
}
@凌驾
未连接的公共无效(@Nullable Bundle){
locationRequest=新的locationRequest();
locationRequest.setInterval(间隔);
locationRequest.SetFastTestInterval(最快间隔);
位置请求。设置最小位移(最小位移);
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
LocationServices.FusedLocationApi.RequestLocationUpdate(GoogleAppClient、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());
}
您可以这样尝试:-
//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());
}
确保您移动的距离大于
最小位移
否则您将无法获得任何位置更新是的,我确信我正在这样做。确保您移动的距离大于最小位移
否则您将无法获得任何位置更新是的,我确信我正在这样做。