我在谷歌地图中有多条折线。当用户点击每条折线时,它必须将其颜色更改为蓝色,而其他折线应为灰色。
我已将折线添加到列表中,如下所示......
polylinePaths = new ArrayList<>();
polylinePaths.add(mMap.addPolyline(polylineOption1));
polylinePaths.add(mMap.addPolyline(polylineOption2));
polylinePaths.add(mMap.addPolyline(polylineOption3));
通过使用OnPolylineClickListener我试图处理每条折线,但无法做到这一点..
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener()
{
@Override
public void onPolylineClick(Polyline polyline)
{
}
});
我的方法是否正确?任何人都可以帮忙完成这项任务吗?
答案 0 :(得分:1)
您可以使用Polyline.setZindex()
来执行此操作:例如,对于未点击的折线,将Z-index设置为1f
,并将Z-index格式设置为1f
(例如2f
})点击。这样的事情:
mGoogleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
// set settings (color, z-index) for all polylines
polyline1.setColor(Color.RED);
polyline1.setZIndex(1f);
polyline2.setColor(Color.RED);
polyline2.setZIndex(1f);
polyline3.setColor(Color.RED);
polyline3.setZIndex(1f);
// set settings (color, z-index) for CLICKED polyline
polyline.setColor(Color.BLUE);
polyline.setZIndex(2f);
}
});
其中polyline1
,polyline2
和polyline3
为:
final List<LatLng> poly1Points = new ArrayList<>();
poly1Points.add(new LatLng(-35.27801,149.12958));
poly1Points.add(new LatLng(-35.28032,149.12907));
poly1Points.add(new LatLng(-35.28099,149.12929));
poly1Points.add(new LatLng(-35.28144,149.12984));
poly1Points.add(new LatLng(-35.28194,149.13003));
poly1Points.add(new LatLng(-35.28282,149.12956));
poly1Points.add(new LatLng(-35.28302,149.12881));
poly1Points.add(new LatLng(-35.28473,149.12836));
PolylineOptions polylineOptions1 = new PolylineOptions()
.addAll(poly1Points)
.color(Color.RED)
.width(20);
final List<LatLng> poly2Points = new ArrayList<>();
poly2Points.add(new LatLng(-35.279472, 149.124717));
poly2Points.add(new LatLng(-35.280146, 149.125908));
poly2Points.add(new LatLng(-35.280453, 149.126380));
poly2Points.add(new LatLng(-35.281775, 149.125286));
poly2Points.add(new LatLng(-35.284245, 149.126649));
poly2Points.add(new LatLng(-35.284604, 149.129814));
poly2Points.add(new LatLng(-35.283395, 149.131209));
poly2Points.add(new LatLng(-35.285576, 149.134964));
PolylineOptions polylineOptions2 = new PolylineOptions()
.addAll(poly2Points)
.color(Color.RED)
.width(20);
final List<LatLng> poly3Points = new ArrayList<>();
poly3Points.add(new LatLng(-35.284211, 149.123073));
poly3Points.add(new LatLng(-35.283624, 149.124586));
poly3Points.add(new LatLng(-35.281750, 149.123610));
poly3Points.add(new LatLng(-35.278711, 149.126163));
poly3Points.add(new LatLng(-35.278107, 149.126345));
poly3Points.add(new LatLng(-35.278536, 149.130626));
poly3Points.add(new LatLng(-35.278737, 149.131484));
poly3Points.add(new LatLng(-35.279516, 149.132031));
poly3Points.add(new LatLng(-35.281478, 149.133179));
poly3Points.add(new LatLng(-35.281478, 149.133179));
PolylineOptions polylineOptions3 = new PolylineOptions()
.addAll(poly3Points)
.color(Color.RED)
.width(20);
final Polyline polyline1 = mGoogleMap.addPolyline(polylineOptions1);
polyline1.setClickable(true);
final Polyline polyline2 = mGoogleMap.addPolyline(polylineOptions2);
polyline2.setClickable(true);
final Polyline polyline3 = mGoogleMap.addPolyline(polylineOptions3);
polyline3.setClickable(true);
并且不要忘记.setClickable(true)
所有折线。结果你应该得到类似的东西(点击的折线是蓝色的并且在顶部):