Because clicking on a polygon always performs the same action - Google maps API android app

0

I have 3 polygons within the same map created in the android application

Polygon nordOption = mMap.addPolygon(new PolygonOptions()
        .add(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p30,p31,p32,p33,p34,p35,p36,p37,p38,p39,p40,p41)
        .strokeColor(Color.RED));

Polygon centreOption = mMap.addPolygon(new PolygonOptions()
  .add(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24)
    .strokeColor(Color.BLUE));

Polygon sudOption = mMap.addPolygon(new PolygonOptions()
.add(s1,s2,s3,s4,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14)
.strokeColor(Color.GREEN));

each one has the click listener activated

nordOption.setClickable(true);
centreOption.setClickable(true);
sudOption.setClickable(true);

The idea is that when you press each polygon, perform a different action, but the only thing I have achieved is to do the same action no matter what polygon you click

mMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
    @Override
    public void onPolygonClick(Polygon polygon) {
        Toast.makeText(getApplication(),"Soy centro",Toast.LENGTH_LONG).show();

    }
});
    
asked by Ernesto Emmanuel Yah Lopez 17.03.2018 в 19:19
source

1 answer

0

To know which polygon we are clicking, we should only use the property of the polygon "id", each polygon has its own id, to recover the id we only use the function

.getId();

Example code

mMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
    @Override
    public void onPolygonClick(Polygon polygon) {

        String zone = polygon.getId();
        switch (zone){
            case "pg3":
                Toast.makeText(getApplication(),"Nord",Toast.LENGTH_LONG).show();
                break;
            case "pg4":
                Toast.makeText(getApplication(),"Centr",Toast.LENGTH_LONG).show();
                break;
            case "pg5":
                Toast.makeText(getApplication(),"sud",Toast.LENGTH_LONG).show();
                break;
        }
    }
});
    
answered by 17.03.2018 в 19:30