本文介绍了如何检测多段线上的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 googlemap 上有一条折线并且在地图上执行了点击,那么我如何检查该点击是在折线上还是其他地方?

If there is a polyline on googlemap and a click is performed on the map, then how can I check whether that click was on polyline or somewhere else?

Polyline line = googleMap.addPolyline(new PolylineOptions()
       .add(new LatLng(51.2, 0.1), new LatLng(51.7, 0.3))
       .width(5)
       .color(Color.RED));

googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {

    }
});

推荐答案

不幸的是,没有折线单击侦听器这样的东西,因此您必须监听地图上的单击并检查是否在任何折线上注册了单击.您还必须保存对添加到地图的多段线的引用.

Unfortunately there's no such thing as a polyline click listener so you'll have to listen to clicks on map and check if a click was registered on any of your polylines. You'll also have to save references to the polylines you added to your map.

这里有一个示例,用于计算距离点击约 100 米处是否存在折线.

Here's an example that calculates if there's a polyline ~100meters away from the click.

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng clickCoords) {
        for (PolylineOptions polyline : mPolylines) {
            for (LatLng polyCoords : polyline.getPoints()) {
                float[] results = new float[1];
                Location.distanceBetween(clickCoords.latitude, clickCoords.longitude,
                        polyCoords.latitude, polyCoords.longitude, results);

                if (results[0] < 100) {
                    // If distance is less than 100 meters, this is your polyline
                    Log.e(TAG, "Found @ "+clickCoords.latitude+" "+clickCoords.longitude);
                }
            }
        }
    }
});

找到多段线后,您可以将该距离保存为float minDistance;,然后循环遍历其他多段线以检查是否有更近的多段线.

Once a polyline is found you can save that distance as float minDistance; and then loop through other polylines to check if there is a closer one.

为了更精确,您可以在每次触摸时获得缩放级别并乘以所需的距离.像 100 * (22 - mMap.getCameraPosition().zoom)(你需要在较低的缩放级别使用更大的距离).

To make this more precise you can get the zoom level at each touch and multiply your required distance. Like 100 * (22 - mMap.getCameraPosition().zoom) (you need to use bigger distance at lower zoom levels).

祝你好运!

这篇关于如何检测多段线上的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:02