两个标记相邻时的Google Map错误标记点击事件 [英] Google Map wrong marker clicked event when two markers are adjacent

查看:92
本文介绍了两个标记相邻时的Google Map错误标记点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为标记应用了自定义PNG图像(大小与默认标记聚类圆的大小相同),这很奇怪.当两个标记接近(但即使它们不重叠)时,我也需要轻按两次标记,因为第一次轻击会错误地给出前一个标记.

I have applied custom PNG images (about the same size as the default marker clusterer circle) for markers and this strange thing happened. When two markers are close (but even when they are not overlapping), I need to tap a marker twice, because the first tap wrongly gives me the previous marker.

也就是说,

  • 有两个标记A和B彼此靠近.
  • 我点击A,然后我得到一个点击标记事件,并带有A.
  • 然后,我点击B,但是我得到了A标记点击事件.
  • 如果我再次选择B,那么我会收到B标记点击事件.

为重现此问题,我创建了一个单独的项目,但未更改标记图像.我将两个标记(我的房子"和车库")彼此靠近放置,并在模拟器中运行该应用程序,并使用鼠标单击准确的位置.我将鼠标放在车库"标记的中心(黑点)上,并一直单击它,而没有移动鼠标.下面是日志.

To reproduce this problem I created a separate project and did not change the marker image. I placed two markers ("My house" and "Garage") close to each other, and run the app in an emulator, and use a mouse to click accurate places. I placed the mouse on the centre (black dot) of the "Garage" marker and kept clicking it without moving the mouse. Below is the log.

[ 12-31 13:42:24.509 28921:28921 D/         ]
Garage is clicked

[ 12-31 13:42:25.664 28921:28921 D/         ]
My house is clicked

[ 12-31 13:42:26.819 28921:28921 D/         ]
Garage is clicked

[ 12-31 13:42:28.066 28921:28921 D/         ]
My house is clicked

[ 12-31 13:42:29.333 28921:28921 D/         ]
Garage is clicked

[ 12-31 13:42:30.503 28921:28921 D/         ]
My house is clicked

如您所见,即使我单击了标记事件的参数完全相同的相同位置,也是如此.这是一个错误吗?

As you see, even though I clicked on the exact same place the marker event's argument kept changing. Is this a bug?

MainActivity

MainActivity

class MainActivity : AppCompatActivity(), OnMapReadyCallback
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val mapFragment = this.supportFragmentManager.findFragmentById(
                R.id.mapView) as SupportMapFragment;
        mapFragment.getMapAsync(this)
    }

    var mMap:GoogleMap? = null;

    override fun onMapReady(p0: GoogleMap?)
    {
        mMap = p0;

        var marker = MarkerOptions()
        marker.position(LatLng(51.501518, -0.141847));
        marker.title("My house");
        p0?.addMarker(marker);

        var marker2 = MarkerOptions()
        marker2.position(LatLng(51.501518, -0.142300));
        marker2.title("Garage");
        p0?.addMarker(marker2);

        p0?.setOnMarkerClickListener {
            marker ->
            Log.d("", marker.title + " is clicked")
            true;
        }
    }
}

XML

<fragment
    android:id="@+id/mapView"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    map:uiZoomControls="true"
    map:cameraTargetLat="51.501518"
    map:cameraTargetLng="-0.141847"
    map:cameraZoom="16"/>

PS:似乎不必那么相邻.我尝试了这些值,但它仍然发生了.

PS: It does not seem to have to be that adjacent. I tried these values, and it still happened.

    marker.position(LatLng(51.501518, -0.141847));
    marker2.position(LatLng(51.501518, -0.142500));

然后,我进一步将两者分开,直到

Then, I further separated the two until,

    marker.position(LatLng(51.501518, -0.141847));
    marker2.position(LatLng(51.501518, -0.142800));

现在,单击中心(黑点)并没有重现该问题.但是,单击靠近另一个标记的制造商的偏心点仍然可以重现该问题.

Now, clicking the centre (black dot) did not reproduce the problem. But clicking on on the off-centre of the maker which is close to the other marker still reproduced the problem.

推荐答案

可以在此处复制100%.我找到了一种解决方法,只需将一个反向的zIndex设置为MarkerOptions.

Can reproduce 100% here. I have found a workaround, just setting an inverted zIndex to the MarkerOptions.

    for (int i = 0; i < locations.size(); i++) {
        Marker marker = mGoogleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .zIndex(locations.size() - 1 - i) // this avoids the bug when the bottom marker is selected
        );
    }

这篇关于两个标记相邻时的Google Map错误标记点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆