无法从android中的谷歌地图中心获取latlong

Nav*_*Nav 3 android google-maps google-maps-markers google-maps-android-api-2

我使用mapFragment在我的应用程序中显示谷歌地图.我想获得地图中心的纬度和经度.

我已将标记图像放在xml文件中的地图中心,

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

     <ImageView  android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/marker_image"
        />
Run Code Online (Sandbox Code Playgroud)

因此,这将标记放置在屏幕的中心,以便用户拖动地图,我应该能够获得地图中心的纬度和经度.

这就是我以前得到的地图中心,

VisibleRegion visibleRegion = googleMap.getProjection()
                    .getVisibleRegion();

Point x = googleMap.getProjection().toScreenLocation(visibleRegion.farRight);

Point y = googleMap.getProjection().toScreenLocation(visibleRegion.nearLeft);

Point centerPoint = new Point(x.x / 2, y.y / 2);

LatLng centerFromPoint = googleMap.getProjection().fromScreenLocation(
                    centerPoint);
Run Code Online (Sandbox Code Playgroud)

问题是我将centerFromPoint LatLng值的值变为0.0.

我甚至试过用

LatLng loc = googleMap.getCameraPosition().target;
Run Code Online (Sandbox Code Playgroud)

即使在这种情况下,我的loc值也是0.0.

我无法弄清楚为什么我没有得到纬度和经度值.

Sim*_*mas 7

您可能在地图尚未加载时尝试获取相机位置.而是与OnMapLoadedCallback听众一起等待,然后获得中心:

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