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

标签 android google-maps google-maps-markers google-maps-android-api-2

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

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

<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"
        />

所以这会将标记放在屏幕的中心,以便用户拖动 map 时我应该能够获得 map 中心的纬度和经度。

这是我用来获取 map 中心的东西,

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

问题是我将 centerFromPoint LatLng 的值设为 0.0。

我什至尝试使用

LatLng loc = googleMap.getCameraPosition().target;

即使在这种情况下,我得到的 loc 值为 0.0。

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

最佳答案

您可能正在尝试在 map 尚未加载时获取相机位置。而是等待 OnMapLoadedCallback 监听器,然后获取中心:

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

关于android - 无法从android中的谷歌地图中心获取latlong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25479650/

相关文章:

android - 如何使用 HttpURLConnection 而不是 Volley 获取 JSON 对象?

java - StartActivityForResult 用于多个按钮访问一个 Activity 并在选择时获取不同的数据?

android studio 加载位图失败

google-maps-api-3 - 带有标记的 Fusion Tables 热图图层

Android,如何从 Google Map V2 中删除所有标记?

java - 寻找 android Facebook SDK 示例

google-maps - 为什么 setMap(null) 不起作用 google maps api v3?

google-maps - 谷歌地图使用three.js和webgl

javascript - 不使用回调方法加载谷歌地图

svg - 如何在 google_maps_flutter Flutter 插件中使用 SVG 标记?