【问题标题】:how to get the precise location of an overlay marker如何获得覆盖标记的精确位置
【发布时间】:2018-04-06 00:08:45
【问题描述】:

我有以下地图,以及使用 OnCameraIdleListener 将相机居中时获得的地址,但正如您在图像中看到的那样,您获得的是标记中心的地址,而不是您想要的底部。 我很感激任何建议。对不起我的英语

图片中的地址应该是“sucre ...”而不是 L.Jaimes ......

@Override
public void onCameraIdle() {

    Log.i(TAG, "onCameraIdle");
    LatLng position = mMap.getCameraPosition().target;
    if (position != null) {
        mCurrentLocation.setLatitude(position.latitude);
        mCurrentLocation.setLongitude(position.longitude);
        retrieveAddress(position);
    }
}

我的地图片段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">


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

    <ImageView
        android:id="@+id/imvCurrentMarker"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_a_marker" />

    </RelativeLayout>

【问题讨论】:

    标签: java android google-maps android-layout google-maps-markers


    【解决方案1】:

    这是这种带有谷歌地图的 UI 的常见问题。但我知道你可以通过耍花招来实现它。

    a) 首先,我建议您使用 Google Map 的 Android SDK 提供的function of Projection。我说的是toScreenLocation(LatLng center)。这个属性帮助我们获得与我们的 LatLng 坐标相对应的确切屏幕点。通过使用这种方法,您可以轻松地将标记置于地图投影的中心,即使您设置了 Padding 并缩小了地图的可见部分。

    现在,有趣的部分是设置可绘制对象的正确位置,以便能够看到它在需要的位置居中,当然还有正确的地址!

    b) 获取视图/可绘制对象的高度和宽度,并且由于可绘制对象位于其中心,因此您必须在该中心设置一个新位置。所以为了实现它,你需要将宽度大小分成两部分。这是因为新位置的重置。

    c) 最后将其向上移动高度大小并水平移动宽度的一半。

        //  a) Get center screenPoint.
        Point screenPoint = mMap.getProjection().toScreenLocation(map.getCameraPosition().target);
        //  b) Get center position of drawable.
        int fixPlaceMarkerPointX = (imvCurrentMarker.getWidth() / 2);
        int fixPlaceMarkerPointY = (imvCurrentMarker.getHeight());
        //  c) Set new position for the marker.
        imvCurrentMarker.setY(screenPoint.y + fixPlaceMarkerPointY);
        imvCurrentMarker.setX(screenPoint.x - fixPlaceMarkerPointX);
    

    d) 我认为还有另一种方法,也是最简单的方法,但并不酷!哈哈 这只是,以负边距移动高度的总大小。 所以你有一个50dp 作为高度,只需给它一个top_margin = "-50dp"。 还有一件事,您可以将 centerInParent 放入标记 Drawable 中。

       <ImageView
        android:id="@+id/imvCurrentMarker"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_a_marker" />
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2018-07-13
      • 2020-09-16
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      相关资源
      最近更新 更多