[英]Move camera to current location OnMapReady (Android Google Maps API)
打开地图后,如何使相机移动到当前用户位置?
这就是我需要位置许可的方式:
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函数中
此代码应将地图居中放置在所需位置。
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.