【发布时间】:2018-06-27 00:29:11
【问题描述】:
我在我的应用中实现了一个谷歌地图片段,用户可以通过点击“指南针”按钮(在右上角)查看他们当前的位置。
这很好用,但是如果用户移动了很远的距离,蓝点就会离开地图。要重新定位蓝点,用户必须再次按下“指南针”按钮。
是否可以跟随蓝点,使其位于地图片段的中心?而当用户移动时,相机也会移动?
以下是用于在片段上显示和更新位置的代码;
//Maps
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Prompt user for permission
getLocationPermission();
//Setup map UI with blue dot etc
updateLocationUI();
// Get the current location of the device and set the position of the map.
getDeviceLocation();
}
private void getLocationPermission() {
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
}
else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
private void updateLocationUI() {
if (mMap == null) {
return;
}
try {
if (mLocationPermissionGranted) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mLastKnownLocation = null;
getLocationPermission();
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
【问题讨论】:
标签: android google-maps android-fragments maps android-gps