【发布时间】:2018-08-25 14:25:08
【问题描述】:
我有以下代码,我可以在启动应用程序时显示我的当前位置。
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 消息。我该如何解决这个问题,让我无论走到哪里,都始终保持在地图的中心?
提前致谢!
【问题讨论】:
-
你在哪里设置你的 LocationListener?
-
@Eselfar 我正在实施它。
class MainActivity extends AppCompatActivity implements LocationListener -
是的,你有一个监听器,它是你的 Activity 本身,因为它实现了接口。但是现在你需要将它设置在某个地方(可能在地图上)才能使用
-
类似
map.setLocationListener(MainActivity.this); -
@Eselfar 如我所见,我的
mMap上没有可以调用的setLocationListener。
标签: android google-maps google-maps-api-3 google-maps-markers locationlistener