【发布时间】: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) 的中心对吗?x和y的值是多少? -
我通过这个链接使用了 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