2016-09-14 68 views
1

我有一個問題,當我嘗試獲取地圖的中心時,它與屏幕的中心不同,並且在不同的設備中存在不同的距離差異。我如何獲得地圖的真正中心?我需要在裏面添加標記谷歌地圖,得到中心不在中心。 Android

感謝您的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; 
} 
+0

你能發表截圖嗎? – antonio

回答

0

我以不同的方式解決了這個問題。

現在我將Im​​ageView的屏幕中的位置(X,Y)轉換爲LatLng到Google地圖!

謝謝大家

1

目標取決於軸承和相機的旋轉。 最好的辦法是通過做來獲得地圖的「中心」的屏幕點,然後將其轉化爲一個地圖點:

myMap.getProjection().fromScreenLocation(screenPoint); 

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

注重的是:

返回與屏幕位置對應的地理位置。 相對於地圖左上方的屏幕像素(不是顯示像素)指定屏幕位置(而不是整個屏幕的左上角)。