如何检测上的折线点击 [英] How to detect a click on a polyline
问题描述
如果有上GoogleMap的折线和点击的地图上进行,那我怎么才能检查点击是否是在折线或别的地方?
折线线= googleMap.addPolyline(新PolylineOptions()
。新增(新的经纬度(51.2,0.1),新的经纬度(51.7,0.3))
.WIDTH(5)
。颜色(Color.RED));
googleMap.setOnMapLongClickListener(新OnMapLongClickListener(){
}
});
不幸的是有没有这样的东西作为一个折线点击监听,所以你要听在地图上点击,并检查是否有点击注册您的任何折线。你还不得不提到保存到您添加到地图中的折线。
下面是一个计算,如果有一条折线〜百米远离点击一个例子。
mMap.setOnMapClickListener(新GoogleMap.OnMapClickListener(){
@覆盖
公共无效onMapClick(经纬度clickCoords){
对于(PolylineOptions折线:mPolylines){
为(经纬度polyCoords:polyline.getPoints()){
浮动[]结果=新的浮动[1];
Location.distanceBetween(clickCoords.latitude,clickCoords.longitude,
polyCoords.latitude,polyCoords.longitude,结果);
如果(结果[0]小于100){
//如果距离小于100米,这是你的折线
Log.e(TAG,发现@+ clickCoords.latitude ++ clickCoords.longitude);
}
}
}
}
});
在一个折线被发现,你可以保存距离为浮动minDistance依旧;
,然后通过其他多段线循环来检查是否有仔细一
为了使这更precise 你可以在每次触摸缩放级别,乘以你所需的距离。像 100 *(22 - mMap.getCameraPosition()变焦)
(你需要使用更大的距离,以较低的缩放级别)
祝你好运!
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.
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);
}
}
}
}
});
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.
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).
Good luck!
这篇关于如何检测上的折线点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!