How to change the marker for the overlay on tap for android?

i have made an application in which i need to change the drawable of an overlayitem when a user clicks on it. i am using the following code to achieve this effect:

        protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);
        if(item.getTitle().equals("true")){
            if(item.getMarker(OverlayItem.ITEM_STATE_FOCUSED_MASK).equals(greenMarker)){
                item.setMarker(orangeMarker);
                view1ComplainPoleList.add(item.getSnippet());
                Log.i("adding",item.getSnippet());
                map.invalidate();
            }
            else{
                item.setMarker(greenMarker);
                view1ComplainPoleList.remove(item.getSnippet());
                Log.i("removing",item.getSnippet());
                map.invalidate();
            }
        }
        return true;
    }

But this does not seem to be working. whenever i click on an overlayitem it disappears from view. what is going wrong?

UPDATE:

could you also tell me how to load new views when the map is scrolled...?

thank you in advance.

Answers


This is very simple to do:

protected boolean onTap(int index) {
    OverlayItem item = mOverlays.get(index);
    //Get the new Drawable
    Drawable marker = mContext.getResources().getDrawable(R.drawable.icon);
    //Set its bounds
    marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
    //Set the new marker
    item.setMarker(marker);
    //Return true! Do not invalidate
    return true;
}

I see a lot of answers here doing this the hard way. If you have 2 images and you want to flip them based on focus, do it the easy way:

Step 1: Copy both images into a drawables folder:

Example: mycon_focused.png, mycon.png

Step 2: Create a selector xml file in drawables: Example "marker.xml"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_focused="true" android:drawable="@drawable/mycon_focused"/>
     <item android:state_focused="false" android:drawable="@drawable/mycon" />
</selector>

Step 3: When you create your ItemOverlay and add the OverlayItems drawable, use

getResources().getDrawable(R.drawable.marker);

instead of

getResources().getDrawable(R.drawable.mycon);

and then programically changing it in the on tap method. After reading through many answers and not seeing this anywhere I just tried it myself, and it worked perfectly.

Much thanks to previous contributors, without your help I wouldn't have had a starting point.

Another note: If you're using Sherif elKhatib suggested code and your marker position is off try:

int width = marker.getIntrinsicWidth();
int height = marker.getIntrinsicHeight();
marker.setBounds(-(width/2),-height,(width/2),0);

instead of

marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());

This should center it perfectly.


I have no idea what view1ComplainPoleList is and whether it is impacting matters. I handled this by subclassing OverlayItem and overriding getMarker() to return the proper image. Here is the sample project in which I use this technique.


Use Setbounds api to set the bounds as follows: Edited your original code

protected boolean onTap(int index) {
    OverlayItem item = mOverlays.get(index);
    if(item.getTitle().equals("true")){
        if(item.getMarker(OverlayItem.ITEM_STATE_FOCUSED_MASK).equals(greenMarker)){
            orangeMarker.setBounds(0,0,orangeMarker.getIntrinsicWidth(),orangeMarker.getIntrinsicHeight());
            item.setMarker(orangeMarker);
            view1ComplainPoleList.add(item.getSnippet());
            Log.i("adding",item.getSnippet());
            map.invalidate();
        }
        else{
            greenMarker.setBounds(0,0,greenMarker.getIntrinsicWidth(),greenMarker.getIntrinsicHeight());
            item.setMarker(greenMarker);
            view1ComplainPoleList.remove(item.getSnippet());
            Log.i("removing",item.getSnippet());
            map.invalidate();
        }
    }
    return true;
}

Need Your Help

FluentMigrator Postgres(Npgsql) quotes

postgresql double-quotes npgsql fluent-migrator

if use fluentmigrator with postgres it generate code like this

Having trouble with Angular.js routes

javascript angularjs

below is what my app.js and controllers.js files look like: