Reputation: 101
I want to show the Toast on a specific polyline coordinates, for example, in this code I want to show the Toast on that specific polyline, however, what happens is that onPolylineClick the Toast is showing the data that is last read by the program, in this case, the 'polylineClicker2()'.
The polyline is placed on its respective coordinates I have given, only the toast is doing what the program last read.
public void polylineClicker() {
LatLng postition1 = new LatLng(14.601796, 121.006707);
LatLng postition2 = new LatLng(14.601033, 120.998558);
PolylineOptions popOption = new PolylineOptions()
.add(postition1).add(postition2).width(5).color(Color.YELLOW).clickable(true);
map.addPolyline(popOption);
map.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
Toast.makeText(MainActivity.this, "Polyline near friends' house", Toast.LENGTH_SHORT).show();
}
});
}
public void polylineClicker2() {
LatLng position1 = new LatLng(14.617106, 120.989924);
LatLng position2 = new LatLng(14.616068, 120.990939);
PolylineOptions popOption = new PolylineOptions()
.add(position1).add(position2).width(5).color(Color.YELLOW).clickable(true);
map.addPolyline(popOption);
map.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
Toast.makeText(MainActivity.this, "Polyline near my house", Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 0
Views: 569
Reputation: 18242
The GoogleMap.OnPolylineClickListener
that you are setting on your polylineClicker2
is overriding the one that you are setting on your polylineClicker
(assuming that you are calling polylineClicker
before polylineClicker2
).
You need to create just one GoogleMap.OnPolylineClickListener
, and as you need different behavior (different messages) you can use tags. Using your code:
public void polylineClicker() {
LatLng position1 = new LatLng(14.601796, 121.006707);
LatLng position2 = new LatLng(14.601033, 120.998558);
PolylineOptions popOption = new PolylineOptions()
.add(position1).add(position2).width(5).color(Color.YELLOW).clickable(true);
map.addPolyline(popOption).setTag("Polyline near friends' house");
}
public void polylineClicker2() {
LatLng position1 = new LatLng(14.617106, 120.989924);
LatLng position2 = new LatLng(14.616068, 120.990939);
PolylineOptions popOption = new PolylineOptions()
.add(position1).add(position2).width(5).color(Color.YELLOW).clickable(true);
map.addPolyline(popOption).setTag("Polyline near my house");
}
And set the GoogleMap.OnPolylineClickListener
wherever you need, for example on your onMapReady
before calling polylineClicker
and polylineClicker2
:
map.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
Toast.makeText(MainActivity.this, (String) polyline.getTag(), Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1