每次位置更改时更新 googlemap 相机位置
Update googlemap Camera location every time location changes
我希望相机在每次位置变化时自动更新和移动。这可能吗?我正在使用 onMapReady() 和 onLocationChanged()。
public class MainActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback {
OnCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) (getSystemService(Context.LOCATION_SERVICE));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PICK_FINE_LOCATION_REQUEST);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PICK_COARSE_LOCATION_REQUEST);
return;
}
locationCurrent = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(locationCurrent);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, this);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
onMapReady()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PICK_FINE_LOCATION_REQUEST);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PICK_COARSE_LOCATION_REQUEST);
return;
}
LatLng latLng = new LatLng(locationCurrent.getLatitude(),locationCurrent.getLongitude());
CameraPosition cameraPosition= new CameraPosition(latLng,15,0,0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
map = googleMap;
我有一个全局变量:Googlemap地图;
onLocationChanged()
@Override
public void onLocationChanged(Location location) {
//Log.d("UPDATEMAP","onLocationChanged");
UpdateMap(map,location);}
我创建了一个名为 UpdateMap() 的方法
public void UpdateMap(GoogleMap map,Location location){
Log.d("UPDATEMAP","UpdateMap");
LatLng latLngUpdate = new LatLng(location.getLatitude(),location.getLongitude());
CameraPosition cameraPosition = new CameraPosition(latLngUpdate,15,0,0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
map.animateCamera(cameraUpdate);
}
我得到的错误是全局变量等于 null,即使我在 onMapReady() 中设置了它。
任何帮助将不胜感激。
谢谢 Ritvik。
您的 map
变量为空,因为 onLocationChanged
在 onMapReady
.
之前被调用
要修复它,请在分配变量后在方法 onMapReady
中请求位置更新。
我希望相机在每次位置变化时自动更新和移动。这可能吗?我正在使用 onMapReady() 和 onLocationChanged()。
public class MainActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback {
OnCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) (getSystemService(Context.LOCATION_SERVICE));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PICK_FINE_LOCATION_REQUEST);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PICK_COARSE_LOCATION_REQUEST);
return;
}
locationCurrent = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(locationCurrent);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, this);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
onMapReady()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//TODO: Consider calling
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PICK_FINE_LOCATION_REQUEST);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PICK_COARSE_LOCATION_REQUEST);
return;
}
LatLng latLng = new LatLng(locationCurrent.getLatitude(),locationCurrent.getLongitude());
CameraPosition cameraPosition= new CameraPosition(latLng,15,0,0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
map = googleMap;
我有一个全局变量:Googlemap地图;
onLocationChanged()
@Override
public void onLocationChanged(Location location) {
//Log.d("UPDATEMAP","onLocationChanged");
UpdateMap(map,location);}
我创建了一个名为 UpdateMap() 的方法
public void UpdateMap(GoogleMap map,Location location){
Log.d("UPDATEMAP","UpdateMap");
LatLng latLngUpdate = new LatLng(location.getLatitude(),location.getLongitude());
CameraPosition cameraPosition = new CameraPosition(latLngUpdate,15,0,0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
map.animateCamera(cameraUpdate);
}
我得到的错误是全局变量等于 null,即使我在 onMapReady() 中设置了它。 任何帮助将不胜感激。 谢谢 Ritvik。
您的 map
变量为空,因为 onLocationChanged
在 onMapReady
.
之前被调用
要修复它,请在分配变量后在方法 onMapReady
中请求位置更新。