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

你说的曾经没有我的故事 提交于 2019-12-02 08:00:44

问题


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()
                                    .findFragmentById(R.id.mapID);
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 VisibleRegion also to get latitude and longitude of center of map but even that did not help.


回答1:


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());
    }
});



回答2:


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;

            }
        });


来源:https://stackoverflow.com/questions/25485941/getting-latitude-longitude-0-0-from-googlemap-getcameraposition-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!