0

Bit of a strange post as this is something I posted before, thought it was solved but didn't!

I have a situation where I need to use a custom Drawable for each marker, the effect I need is to rotate the marker by a set number of degrees and am probably going the wrong way about this completely. 

As a start I am using the excellent Creative Commons as this does work and only breaks after my additions!

https://github.com/commonsguy/cw-advandroid/tree/master/Maps/NooYawkAsync

The previous post and the code is here 

Android Maps NullPointerException ItemizedOverlay

If anyone can recommend a better and more stable way to get a rotated Drawable and put me on the right path that would be great.

As you can see from my code I don't set the Drawable in the overlay and do this outside within another object and suspect this is why it is breaking!

At the moment I set my Drawable by doing this...

public Drawable getRotatedIcon(String string) {
        Drawable marker = null;
        Double tempHeading = Double.valueOf(string);
        long intHeading = Math.round(tempHeading / 10);

        int resID = getResources().getIdentifier(
                "icon_rotate_" + Long.toString(intHeading), "drawable",
                "com.test.testapp");

        marker = getResources().getDrawable(resID);
        marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
        return marker;

    }

And then to create it all I use this...

if (sites != null){
            sites.clearItems(); 
        }else{
            sites = new SitesOverlay();
        }

        for (int i = 0; i < getMainOverlayArray().size(); i++) {
            tempOver = getMainOverlayArray().get(i);
            tempOver.setMarker(getRotatedIcon(tempOver.getcourse()));
            if (tempOver != null){ 
                sites.addItem(tempOver);
            }
        }

        sites.finishedLoading();

And my SitesOverlay class...

     private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> {
        private List<pfOverlayItem> items = new ArrayList<pfOverlayItem>();

        private PopupPanel panel=new PopupPanel(R.layout.popup);
        private MapLocation selectedMapLocation;  
        private static final int CIRCLERADIUS = 2;
        private ArrayList<pfOverlayItem> mOverlays = new ArrayList<pfOverlayItem>();



        public SitesOverlay() {

            super(null);
            populate();

        }



public void finishedLoading(){

    populate();

}

        @Override
        protected pfOverlayItem createItem(int i) {
            return (items.get(i));

        }

        public void addItem(OverlayItem overlay) {
            overlay.setMarker(boundCenter(overlay.getMarker(0)));
            items.add((pfOverlayItem) overlay);
        }

        public void clearItems(){
            runOnUiThread(new Runnable() {
                public void run() {
                    items.clear();
                    myMapView.invalidate();
                }
            });


        }

        public void clear() {
            mOverlays.clear();
            myMapView.removeAllViews();

            setLastFocusedIndex(-1);
            populate();
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, false);

            try {


            if (getMainOverlayArray().size() != 0){

            pfOverlayItem tempOver = null;
            for (int i = 0; i < getMainOverlayArray().size(); i++) {

                tempOver = getMainOverlayArray().get(i);

            boolean isMatch = false;

            //Log.i("Selected Name",selectedName);

            if (tempOver.getTitle().equals(selectedName))
            {
                isMatch = true;
            }

            if (isMatch){

            Projection projection = mapView.getProjection();
            Point point = new Point();
            projection.toPixels(tempOver.getPoint(), point);

            Paint background = new Paint();
            background.setColor(Color.WHITE);
            background.setAlpha(150);
            RectF rect = new RectF();
            rect.set(point.x - 50, point.y +15,
                    point.x + 90, point.y + 50);
            canvas.drawRoundRect(rect, 5, 5, background);


            Paint text = new Paint();
            text.setAntiAlias(true);
            text.setColor(Color.BLUE);
            text.setTextSize(14);
            text.setTypeface(Typeface.MONOSPACE);

            canvas.drawText(tempOver.getTitle() + " " + tempOver.getcallsign(), point.x -50 , point.y + 30, text);
            canvas.drawText(tempOver.getdestination() + " " + tempOver.getdraft(), point.x -50 , point.y + 45, text);
            }
            }
            }
            }catch (Exception e){
                Log.e("Error", "Problem drawing view", e);
                 e.printStackTrace();

            }
        }

        @Override
        protected boolean onTap(int i) {

            pfOverlayItem item = getItem(i);

            if (selectedName.equals(item.getTitle())){

                try{    
                Intent myIntent = new Intent(TestApp.this, DetailActivity.class);
                myIntent.putExtra( "int", i);
                myIntent.putExtra( "string", selectedName );
                PlaneFinderMain.this.startActivity(myIntent);
                }catch (Exception e){
                    Log.e("Error", "Cannot launch", e);
                }
            }

            currentadshex = item.getmmsi();
            new GetRouteTask(item.getmmsi()).execute();

            selectedItem = i;
            selectedName = item.getTitle();
            selectedPlanePoint = item.getPoint();

            GeoPoint geo=item.getPoint();
            Point pt=myMapView.getProjection().toPixels(geo, null);

            View view=panel.getView();

            ((TextView)view.findViewById(R.id.reg)).setText(item.getTitle());
            ((TextView)view.findViewById(R.id.callsign)).setText(item.getcallsign());
            ((TextView)view.findViewById(R.id.dest)).setText(item.getdestination());
            ((TextView)view.findViewById(R.id.draft)).setText(item.getdraft());

            return (true);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {

             if (event.getAction() == MotionEvent.ACTION_DOWN){

                 if (selectedPlanePoint != null){
                     Projection projection = mapView.getProjection();
                     Point point = new Point();
                     projection.toPixels(selectedPlanePoint, point);

                     Point pointHit = new Point();
                     pointHit.x=(int)event.getX();
                     pointHit.y=(int)event.getY();

                     if ((point.x - pointHit.x) >-100 && (point.x - pointHit.x) <70  && (point.y - pointHit.y) < -25  && (point.y - pointHit.y) > -95){

                            try{    
                                 Intent myIntent = new Intent(TestApp.this, DetailActivity.class);

                                    myIntent.putExtra( "int", selectedItem);
                                    myIntent.putExtra( "string", selectedName );
                                    TestApp.this.startActivity(myIntent);

                                }catch (Exception e){
                                    Log.e("Error", "Cannot launch", e);
                                }

                     }else{

                     }


                 }


             }



            return false;


        }



        @Override
        public int size() {
            return (items.size());
        }

        public void addOverlay(OverlayItem o){
            setLastFocusedIndex(-1);
            populate();
        }



    }
Community
  • 1
  • 1
Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122

2 Answers2

6

It seems the natural API choice, RotateDrawable, is braindead in that you can only use it from inflated layout. I have adapted Mark Murphy's code referenced in the question into the below activity which shows the New York markers spinning, using an alternative RotateDrawable which wraps a contained drawable in a more flexible way:

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
import java.util.List;

public class NooYawk extends MapActivity {
    private MapView map=null;
    private MyLocationOverlay me=null;
    private SitesOverlay sites=null;
    Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map=(MapView)findViewById(R.id.map);

        map.getController().setCenter(getPoint(40.76793169992044, -73.98180484771729));
        map.getController().setZoom(17);
        map.setBuiltInZoomControls(true);

        me=new MyLocationOverlay(this, map);
        map.getOverlays().add(me);

        sites=new SitesOverlay();
        map.getOverlays().add(sites);
        map.invalidate();           
    }

    @Override
    public void onResume() {
        super.onResume();

        me.enableCompass();
    }       

    @Override
    public void onPause() {
        super.onPause();

        me.disableCompass();
    }       

    @Override
    protected boolean isRouteDisplayed() {
        return(false);
    }

    private GeoPoint getPoint(double lat, double lon) {
        return(new GeoPoint((int)(lat*1000000.0), (int)(lon*1000000.0)));
    }

    private class SitesOverlay extends ItemizedOverlay<CustomItem> {
        private List<CustomItem> items=new ArrayList<CustomItem>();

        public SitesOverlay() {
            super(null);
            items.add(new CustomItem(getPoint(40.748963847316034, -73.96807193756104),
                    "UN", "United Nations",getMarker(R.drawable.blue_full_marker)));
            items.add(new CustomItem(getPoint(40.76866299974387, -73.98268461227417),
                    "Lincoln Center", "Home of Jazz at Lincoln Center", getMarker(R.drawable.orange_full_marker)));
            items.add(new CustomItem(getPoint(40.765136435316755, -73.97989511489868), 
                    "Carnegie Hall", "Where you go with practice, practice, practice", getMarker(R.drawable.green_full_marker)));
            items.add(new CustomItem(getPoint(40.70686417491799,-74.01572942733765), "The Downtown Club",
                    "Original home of the Heisman Trophy", getMarker(R.drawable.purple_full_marker)));
            populate();
        }

        @Override
        protected CustomItem createItem(int i) {
            return(items.get(i));
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, shadow);
            handler.post(new Runnable() {
                @Override 
                public void run() {
                    for (CustomItem item : items) {
                        item.marker.rotate();
                    }
                    map.invalidate();
                }               
            });
        }

        @Override
        public int size() {
            return(items.size());
        }


        private Drawable getMarker(int resource) {
            Drawable marker=getResources().getDrawable(resource);

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

            return(marker);
        }
    }

    //
    // A RotateDrawable that isn't braindead.
    //
    private class RotateDrawable extends Drawable {
        private Drawable mDrawable;
        private float mPivotX = 0.5f;
        private float mPivotY = 0.5f;
        private float mCurrentDegrees = 0f;
        public RotateDrawable(Drawable drawable) {
            this.mDrawable = drawable;
        }
        public void rotate() {
            mCurrentDegrees+=5;
            if (mCurrentDegrees>=360) {
                mCurrentDegrees-=360;
            }
        }
        public void draw(Canvas canvas) {
            int saveCount = canvas.save();
            Rect bounds = mDrawable.getBounds();
            int w = bounds.right - bounds.left;
            int h = bounds.bottom - bounds.top;
            float px = w * mPivotX;
            float py = h * mPivotY;
            canvas.rotate(mCurrentDegrees, 0, 0);
            mDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        }

        @Override public int getOpacity() {
            return mDrawable.getOpacity();
        }

        @Override
        public void setAlpha(int arg0) {
            mDrawable.setAlpha(arg0);
        }

        @Override
        public void setColorFilter(ColorFilter arg0) {
            mDrawable.setColorFilter(arg0);
        }
    }

    class CustomItem extends OverlayItem {
        public RotateDrawable marker=null;
        CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
            super(pt, name, snippet);
            this.marker=new RotateDrawable(marker);
        }


        @Override
        public Drawable getMarker(int stateBitset) {
            Drawable result=(marker);

            setState(result, stateBitset);

            return(result);
        }
    }

}

Any questions, feel free to ask. :-)

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • That looks great Reuben and one I think that has some mileage indeed! I suppose the best way for each Marker to have a different angle is to pass an extra parameter into this.marker=new RotateDrawable(marker); ?? – Lee Armstrong Feb 01 '11 at 14:35
  • 1
    Oh exactly. I just made them rotate by 5 degrees each frame in RotateDrawable.rotate() to show it worked... you don't have to keep that function. You can also lose the unused mPivotX and mPivotY fields btw... they're not needed. – Reuben Scratton Feb 01 '11 at 14:49
  • OK thanks, I suppose I also take out the run() as I don't need to animate the roatation, it will be a one time rotation of the item, no animation needed at all. – Lee Armstrong Feb 01 '11 at 14:51
  • Absolutely. Lose the Handler and the runnable... that was all just to demo the approach by making them spin. – Reuben Scratton Feb 01 '11 at 14:58
  • @LeeArmstrong How are you getting on with this? – Reuben Scratton Feb 03 '11 at 12:11
  • I've not tried anything yet and was waiting to see if anyone else had anything to offer for that nice bounty :-) – Lee Armstrong Feb 03 '11 at 12:12
  • It certainly is a nice bounty. I feel you might have overbountied in fact, so if you have any related questions (or even unrelated questions) feel free to ask here and you'll have my full attention. :) – Reuben Scratton Feb 03 '11 at 12:19
  • If either of you guys are still using SO would you know why the above doesn't actually draw the marker on the map? I've spent hours on this and your explanation makes by far the most sense but just doesn't quite work? – Apqu Dec 10 '13 at 13:08
0

In that case Lee I suggest you give a try to AnimationDrawable

    AnimationDrawable frameAnimation = 
(AnimationDrawable) getResources().getDrawable(R.drawable.spin_animationm);

EDIT: In the meantime came over another useful post ' moving-icons-on-a-android-mapview' which suggests using a timer thread in the onDraw().

Hope that helps.

Community
  • 1
  • 1
100rabh
  • 6,156
  • 5
  • 27
  • 41
  • But that is for animating them? I want to rotate each one by a different number of degrees? – Lee Armstrong Feb 01 '11 at 07:53
  • Lee for rotation you may consider using RotateDrawable but I have never set its angles programmatically. – 100rabh Feb 01 '11 at 08:02
  • Yes well I have it working using a more lightweight method by pre-rotating so the OS doesn't need to. It crashes infrequently but enough to annoy users so it must be something small. What you are suggesting I have tried and it hammers the CPU. – Lee Armstrong Feb 01 '11 at 08:36