每次更改位置时更新googlemap相机位置

时间:2018-01-22 23:44:39

标签: android google-maps locationlistener

我希望相机在每次位置变化时自动更新和移动。这可能吗?我正在使用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 map;

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。

1 个答案:

答案 0 :(得分:1)

您的map变量为空,因为在onLocationChanged之前调用了onMapReady 在分配变量后,在方法onMapReady中修复请求位置更新。