Google 地图中的相机不跟随用户当前位置
Camera does not follow user current location in Google Maps
我有以下代码,可以在启动应用程序时显示我的当前位置。
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();
}
}
}
}
}
所以每次我移动时,我都不会保持在地图的中心。要再次将我置于地图中心,我需要按定位按钮。我已经从 LocationListener
接口实现了 onLocationChanged()
方法,但没有任何反应。甚至不显示 Toast 消息。我怎样才能解决这个问题,让我走到哪里,始终保持在地图的中心?
提前致谢!
您正在关注 this tutorial to implement the location, as mentioned at the end of it, you need to follow this one 以更新新位置。
创建回调:
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
// ...
}
};
};
设置listener/callback:
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback,
null /* Looper */);
我有以下代码,可以在启动应用程序时显示我的当前位置。
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();
}
}
}
}
}
所以每次我移动时,我都不会保持在地图的中心。要再次将我置于地图中心,我需要按定位按钮。我已经从 LocationListener
接口实现了 onLocationChanged()
方法,但没有任何反应。甚至不显示 Toast 消息。我怎样才能解决这个问题,让我走到哪里,始终保持在地图的中心?
提前致谢!
您正在关注 this tutorial to implement the location, as mentioned at the end of it, you need to follow this one 以更新新位置。
创建回调:
mLocationCallback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { if (locationResult == null) { return; } for (Location location : locationResult.getLocations()) { // Update UI with location data // ... } }; };
设置listener/callback:
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null /* Looper */);