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

前端 未结 2 1581
梦毁少年i
梦毁少年i 2021-01-24 01:53

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) getSupport         


        
相关标签:
2条回答
  • 2021-01-24 02:22

    Before you get these values, You must declare this Listener

    • setOnCameraMoveListener : while 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;
    
                }
            });
    
    0 讨论(0)
  • 2021-01-24 02:34

    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() {
        @Override
        public void onMapLoaded() {
            Log.e("TAG", googleMap.getCameraPosition().target.toString());
        }
    });
    
    0 讨论(0)
提交回复
热议问题