【问题标题】:Google maps, get center is not on the center. Android谷歌地图,获取中心不在中心。安卓
【发布时间】:2016-09-14 11:52:52
【问题描述】:

我有一个问题,当我尝试获取地图的中心时,它与屏幕的中心不同,并且在不同的设备中存在不同的距离差异。我怎样才能获得地图的真正中心?我需要在其中添加一个标记

谢谢

部分 XML 代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map" tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <ImageView

        android:layout_width="64dp"
        android:layout_height="64dp"
        android:id="@+id/crosshair"
        android:src="@drawable/crosshair"
        android:onClick="btPontoClick"
        android:layout_centerInParent="true"/>

    </RelativeLayout>

获取中心的Java代码:

public void btPontoClick(View v){
    LatLng latlng =  mMap.getCameraPosition().target;
    addPonto(latlng);
    okParaVoltar = false;
}

【问题讨论】:

  • 可以发截图吗?

标签: java android google-maps maps


【解决方案1】:

目标取决于相机的方位和旋转。 最好的方法是获取地图“中心”的屏幕点,然后通过执行以下操作将其转换为地图点:

myMap.getProjection().fromScreenLocation(screenPoint);

https://developers.google.com/android/reference/com/google/android/gms/maps/Projection

注意:

返回与屏幕位置对应的地理位置。 屏幕位置以相对于地图左上角(而不是地图左上角)的屏幕像素(不是显示像素)为单位指定 整个屏幕)。

【讨论】:

    【解决方案2】:

    我以不同的方式解决了这个问题。

    现在我将 ImageView 屏幕中的 Position(X,Y) 转换为 LatLng 到谷歌地图!

    谢谢大家

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2013-05-19
      • 2013-11-26
      相关资源
      最近更新 更多