我使用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。
我无法弄清楚为什么我没有得到纬度和经度值。
答案 0 :(得分:7)
当地图尚未加载时,您可能正在尝试获取相机位置。而是使用OnMapLoadedCallback
侦听器等待,然后获取中心:
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
Log.e("TAG", googleMap.getCameraPosition().target.toString());
}
});