[英]Google maps, get center is not on the center. Android
I have a problem that when i try to get the center of the map it is different from the center of the screen and in different devices there are different distance differences.. How can i get the true center of the map?我有一个问题,当我尝试获取地图中心时,它与屏幕中心不同,并且在不同的设备中,距离差异也不同.. 我怎样才能获得地图的真正中心? I need to add a marker in it
我需要在其中添加一个标记
Thank you谢谢
Part of the XML code:部分 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 code that get the center:获取中心的Java代码:
public void btPontoClick(View v){
LatLng latlng = mMap.getCameraPosition().target;
addPonto(latlng);
okParaVoltar = false;
}
Target depends on the bearing and rotation of the camera.目标取决于相机的方位和旋转。 The best way is to get the screen point of the "center" of the map and then translate it to a map point by doing:
最好的方法是获取地图“中心”的屏幕点,然后通过执行以下操作将其转换为地图点:
myMap.getProjection().fromScreenLocation(screenPoint);
https://developers.google.com/android/reference/com/google/android/gms/maps/Projection https://developers.google.com/android/reference/com/google/android/gms/maps/Projection
Pay attention that:请注意:
Returns the geographic location that corresponds to a screen location.
返回与屏幕位置对应的地理位置。 The screen location is specified in screen pixels (not display pixels) relative to the top left of the map (not the top left of the whole screen).
屏幕位置以相对于地图左上角(不是整个屏幕的左上角)的屏幕像素(不是显示像素)指定。
I solved the problem in a different way.我以不同的方式解决了这个问题。
Now I convert the Position(X,Y) in the screen of a ImageView to LatLng to google maps!现在我将 ImageView 屏幕中的 Position(X,Y) 转换为 LatLng 到谷歌地图!
Thank you all谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.