Getting the longitude of latitude 0.0 from googleMap.getCameraPosition (). target

I was trying to get the latitude and longitude of the center of the map. I am using Fragment to display the map which is,

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                                    .findFragmentById(R.id.mapID);
if (fm != null) {
    googleMap = fm.getMap();
}
LatLng loc = googleMap.getCameraPosition().target;

      

I print this loc and I get the value as 0.0, 0.0. I don't understand where I am going wrong.

I tried to use VisibleRegion

also to get the latitude and longitude of the center of the map, but even that didn't help.

+1


source to share


2 answers


You are asking for the position of the camera when the map is not loaded yet, so the camera points to 0.0, 0.0. Use this instead:



googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        Log.e("TAG", googleMap.getCameraPosition().target.toString());
    }
});

      

+5


source


Before you can get these values, you must declare this listener

  • setOnCameraMoveListener: while the camera is moving.

  • setOnCameraIdleListener: after ceme



or any listener like this.

GoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                double CameraLat = map.getCameraPosition().target.latitude;
                double CameraLong = map.getCameraPosition().target.longitude;

            }
        });

      

+1


source







All Articles