Naveen Kumar Naveen Kumar - 10 months ago 75
Android Question

getting latitude longitude 0.0 from googleMap.getCameraPosition().target

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

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
if (fm != null) {
googleMap = fm.getMap();
LatLng loc = googleMap.getCameraPosition().target;

I am printing this loc and i am getting value as 0.0, 0.0. I am not getting where i am going wrong.

I tried to use
also to get latitude and longitude of center of map but even that did not help.

Answer Source

You are requesting the camera position when the map was not yet loaded, therefore the camera is pointing to 0.0, 0.0. Instead use this:

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