【问题标题】:Unable to get latlong from center of the google map in android无法从 android 中的谷歌地图中心获取 latlong
【发布时间】:2014-08-25 05:45:13
【问题描述】:

我使用 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"
        />

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

这是我用来获取地图中心的,

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。

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

【问题讨论】:

  • 据我所知,您应该重新检查如何计算 centerPoint 因为您实际上应该有四个点来确定中心(2 个点来计算沿 x 的中心,2 个点来计算y) 的中心对吗? xy 的值是多少?
  • 我通过这个链接使用了 visibleregion 方法stackoverflow.com/questions/13904505/…
  • 尝试打印出 x.x 和 x.y。顺便说一句,您为什么不尝试该问题的第一个答案?(对我来说似乎更直接)
  • @shyam 我尝试从相机位置使用第一种方法本身,但我得到了 lat long 0.0 所以尝试了可见区域。我试图打印积分,我得到的积分只有 0。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多