简体   繁体   中英

How to set validation of marker's color in android (Google Map API)

So, I want to set validation base on marker's color.

First, this is my code of marker's looping, so it will show markers (This is not the main problem, this code works)

for(int i = 0; i < datMarkerList.size(); i++)
    {
        if(i < 50) {
            DAT_MARKER datMarker = datMarkerList.get(i);
            marker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(datMarker.getLATITUDE(), datMarker.getLONGITUDE()))
                    .title(datMarker.getDESCRIPTION())
                    .snippet(datMarker.getID_MARKER() + ""));
            markers.add(marker);
        } else {
            continue;
        }

        DAT_MARKER_OP datMarkerOp = datMarkerOpList.get(i);
        if(datMarkerOp.getKODE_PAJAK() == 0) { //KODE PAJAK HOTEL
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        }else if(datMarkerOp.getKODE_PAJAK() == 1) { //KODE PAJAK RESTAURANT
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
        }else { //KODE PAJAK HOTEL DAN RESTAURANT
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        }
    }

Then, from those icon, I want to make validation base on those colors, so I think I will set it here (also I put the algorithms) :

@Override
public boolean onMarkerClick(final Marker marker) {
    linCard.setVisibility(View.VISIBLE);
    //mapSettings.setMyLocationButtonEnabled(false);
    linCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //IF marker's color is RED
                //Statement
            //IF marker's color is YELLOW
                //Statement
            //IF marker's color is BLUE
                //Statement

        }
    });

That's it. I've been thinking and I have no idea about the code, any ideas?

我认为您可以使用标记的标题进行标识,也可以使用Hashmap在其中保存制造商及其相应的信息,例如http://bon-app-etit.blogspot.be/2012/12/add- informationobject-to-marker-in.html

You don't need to mix your app logic with colors.
You want to do some action based on marker type, not it's color. Colors can be changed.
So, you need to link you additional info to markers, for example through the Map.

Map<Marker,DAT_MARKER_OP> markersMap = new HashMap<>();

And in your marker's looping:

markersMap.put(marker, datMarkerOp);

After this you can get your DAT_MARKER_OP in onClick

public boolean onMarkerClick(final Marker marker) {
    linCard.setVisibility(View.VISIBLE);
    linCard.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            DAT_MARKER_OP datMarkerOp = markersMap.get(marker);
            if(datMarkerOp.getKODE_PAJAK() == 0) {
                // your logic
            }

        }
    });
}

You have 2 identifiers here.. Title and snippet can use either of them to distinguish the markers.for eg give id_red for all the red colored markers ,id_yellow for all the yellow colored markers

if(marker.getTitle().toString().equals(id_red )){
        its a red colored marker
 }else{

 } 

Or

You can use snippet for the same thing

 if (marker.getSnippet().contains("id_red")) {
     red
    } else{
    // Click of another colored marker
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM