Search code examples
javaandroidosmdroid

How to extends ResourceProxy interface in osmdroid?


yea, i know, another thread about changing icons in osmdroid. But there isn't any good explanation available !

I made lot of research and it is a frequently asked topic. Why not make a clear answer at this point for all user who need to specify their own resources ?

All stuff available is to "Have a look at ResourceProxy.java" or "Pass the bitmap to constructor". No way, it doesn't work for me, I don't even know how a simple interface could find my drawable in my res folder ! I tried to pass some .PNG files with the same names as osmdroid (like "person.png") but when I run the app I have still the default resource.

Can someone explain clearly how to write "our own ResourceProxy" step by step ? Not brievely like that because I followed the steps and didn't manage to success.

I know that osmdroid was fixed recently in github but the .aar is older than the fix. Here you can find the Resource Proxy class.


Solution

  • When you create in an instance of the MapView, you need to pass in a reference to a implementation of "ResourceProxy". By default, it loads the "DefaultResourceProxyImpl". To override, create a new class that extends DefaultResourceProxyImpl and then you can override getBitmap and getDrawable. There is actually is an example that overrides the DefaultResourceProxyImpl here:

    https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/ResourceProxyImpl.java

    Just for you, I'm working on including such an example with the osmdroid example app. It's going to look something like this

     public class CustomResourceProxy extends DefaultResourceProxyImpl {
    
          private final Context mContext;
          public CustomResourceProxy(Context pContext) {
               super(pContext);
            mContext = pContext;
          }
    
          @Override
        public Bitmap getBitmap(final bitmap pResId) {
            switch (pResId){
                    case person:
                         //your image goes here!!!
                         return BitmapFactory.decodeResource(mContext.getResources(),org.osmdroid.example.R.drawable.sfgpuci);
    
               }
               return super.getBitmap(pResId);
        }
    
        @Override
        public Drawable getDrawable(final bitmap pResId) {
            switch (pResId){
                    case person:
                         //your image goes here!!!
                         return mContext.getResources().getDrawable(org.osmdroid.example.R.drawable.sfgpuci);
               }
               return super.getDrawable(pResId);
        }
    
     }
    

    Edit: done See https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/CustomResourceProxy.java

    Edit: Edit: Wiki updated, see https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library