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