Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3844944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:10:06+00:00 2026-05-19T16:10:06+00:00

Bit of a strange post as this is something I posted before, thought it

  • 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();
        }



    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:10:06+00:00Added an answer on May 19, 2026 at 4:10 pm

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling my problem is a bit strange, but here goes... I
I think this one is silly question, but it was a bit strange for
I'm experiencing something a bit strange. I have a table on SQL Server 2008,
I have working code but it seems a bit strange that I have to
This one may be a bit strange. I have a solution in Visual Studio
This post might seem a bit strange. The thing is im experimenting with new
A co-worker said this is possible (but it looks a bit strange to me).
Ok, so this one is a bit strange.... I have an HTML page in
I have maybe a little bit strange task, but I belive there is no
This problem is a bit strange. Why is showed Is not null, if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.