Google Map wrong marker clicked event when two markers are adjacent

让人想犯罪 __ 提交于 2019-12-21 19:47:48

问题


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.

That is,

  • There are two markers A and B close to each other.
  • I tap A, and then I get a marker clicked event with A.
  • Then, I tap B, but I get a marker clicked event with A.
  • If I tab B again, then I get a marker clicked event with 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

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: 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.


回答1:


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
        );
    }


来源:https://stackoverflow.com/questions/48040110/google-map-wrong-marker-clicked-event-when-two-markers-are-adjacent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!