我已经使用mapFragment在我的应用程序中显示google map。我想获取地图中心的纬度和经度。
我已将标记图像放置在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。
我无法弄清楚为什么我没有得到经度值。
您可能正在尝试获取尚未加载地图的摄像机位置。取而代之的是与OnMapLoadedCallback
听众等待,然后获得中心:
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
Log.e("TAG", googleMap.getCameraPosition().target.toString());
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句