Android 相机不跟随用户在谷歌地图中的当前位置

Android 相机不跟随用户在谷歌地图中的当前位置,android,google-maps,google-maps-api-3,google-maps-markers,locationlistener,Android,Google Maps,Google Maps Api 3,Google Maps Markers,Locationlistener,我有以下代码,可以在启动应用程序时显示我的当前位置 public class MainActivity extends AppCompatActivity implements LocationListener { //private fields @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我有以下代码,可以在启动应用程序时显示我的当前位置

public class MainActivity extends AppCompatActivity implements LocationListener {
   //private fields

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

        if (isGooglePlayServicesOk()) {
            getLocationPermissions();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        this.location = location;
    //NO TOAST MESSAGE - NOTHING HAPPENS <=============
    Toast.makeText(context, location.getLatitude() + " / " + location.getLongitude(), Toast.LENGTH_SHORT).show();
    }

    //Other three empty methods onStatusChanged, onProviderEnabled and onProviderDisabled

    private void getDeviceLocation() {
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        try {
            if (locationPermissionsGranted) {
                Task task = fusedLocationProviderClient.getLastLocation();
                task.addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            location = (Location) task.getResult();
                            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18f));
                        }
                    }
                });
            }
        } catch (SecurityException se) {
            Log.d(TAG, se.getMessage());
        }
    }

    public boolean isGooglePlayServicesOk() {
        //Method that check Google Play Services
    }

    private void initializeMap() {
        SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        supportMapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;

                if (locationPermissionsGranted) {
                    getDeviceLocation();
                    if (/* Manifest Conditions */) {return;}
                    mMap.setMyLocationEnabled(true);
                }
            }
        });
    }

    private void getLocationPermissions() {
        String[] permissions = {COARSE_LOCATION, FINE_LOCATION};
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(), COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationPermissionsGranted = true;
                initializeMap();
            } else {
                ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE);
            }
        } else {
            ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        locationPermissionsGranted = false;

        switch (requestCode) {
            case LOCATION_PERMISSION_REQUEST_CODE: {
                if (grantResults.length > 0) {
                    for (int grantResult : grantResults) {
                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
                            locationPermissionsGranted = false;
                            return;
                        }
                    }
                    locationPermissionsGranted = true;
                    initializeMap();
                }
            }
        }
    }
}
public类MainActivity扩展AppCompatActivity实现LocationListener{
//私人领域
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isGooglePlayServicesOk()){
getLocationPermissions();
}
}
@凌驾
已更改位置上的公共无效(位置){
这个位置=位置;
//没有TOAST消息-什么也没有发生(0){
for(int grantResult:grantResults){
if(grantResult!=已授予PackageManager.PERMISSION){
locationPermissionsGranted=错误;
返回;
}
}
locationPermissionsGranted=真;
初始化映射();
}
}
}
}
}
所以每次我移动的时候,我都不会保持在地图的中心。要再次将我置于地图的中心,我需要按下位置按钮。我已经从
LocationListener
接口实现了
onLocationChanged()
方法,但什么也没发生。甚至连祝酒词都没有显示。我该如何解决这个问题,无论我走到哪里,都要始终保持在地图的中心

提前谢谢

正如本文末尾提到的,当您跟随以实现位置时,您需要跟随以使用新位置进行更新

创建回调:

设置侦听器/回调:


您在哪里设置LocationListener?@Eselfar我正在实现它
class MainActivity扩展AppCompatActivity实现LocationListener
Yeah您有一个监听器,它是您的活动本身,因为它实现了接口。但是现在您需要将它设置在某个地方(可能在地图上)以便使用,比如
Map.setLocationListener(MainActivity.this)
@Eselfar如我所见,在我的
mMap
上没有可以调用的
setLocationListener
mLocationCallback = new LocationCallback() {
  @Override
  public void onLocationResult(LocationResult locationResult) {
      if (locationResult == null) {
          return;
      }
      for (Location location : locationResult.getLocations()) {
          // Update UI with location data
          // ...
      }
  };
};
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
  mLocationCallback,
  null /* Looper */);