如果地图打开,如何让相机移动到当前用户位置? 这就是我要求获得该位置的权限:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_LOCATION_REQUEST_CODE);
}
这是onCreate函数
答案 0 :(得分:0)
此代码应将地图置于中心位置,您需要地理位置。
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(**LatLng object of your position**) // Sets the center of the map to Geolocation
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
修改强> 首先,我们在顶部创建一些变量:
private boolean localizationAllowed; //here we save if the user has allowed to locate him.
private boolean localizationRequested = false; // here we save if the localization has already requested.
创建一些私有方法:
private void enableLocationUpdates(String provider) {
if(!localizationRequested) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATIONS);
localizationRequested = true;
}
}
if (provider != null && localizationAllowed) {
locationManager.requestLocationUpdates(provider, 1000, 0, this);
}
}
private void disableLocationUpdates() {
if(!localizationRequested) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATIONS);
localizationRequested = true;
}
}
if (locationManager != null && localizationAllowed){
locationManager.removeUpdates(this);
}
}
private void initLocalisation(){
if(localizationAllowed) {
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
if (service != null) {
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
enableLocationUpdates(provider);
}
}
}
初始化本地化:
@Override
protected void onCreate(Bundle savedInstanceState) {
//your onCreate methods
initLocalisation();
//your onCreate methods
}
onMapReady在地图准备好后开始本地化
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Location location = getLastLocation();
if (location != null) {
LatLng you = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(you, 17f));
}
}