如何在osmdroid中扩展ResourceProxy接口?

时间:2015-10-17 11:13:32

标签: java android osmdroid

是的,我知道,另一个关于在osmdroid中更改图标的主题。 但是没有任何好的解释!

我做了很多研究,它是frequently asked topic。为什么不对所有需要指定自己资源的用户做出明确答复呢?

所有可用的东西都是“Have a look at ResourceProxy.java”或“将位图传递给构造函数”。没办法,它对我不起作用,我甚至不知道一个简单的界面如何在我的res文件夹中找到我的drawable!我试图传递一些与osmdroid同名的.PNG文件(如“person.png”),但是当我运行应用程序时,我仍然是默认资源。

有人可以解释明确如何逐步编写“我们自己的ResourceProxy ”吗? Not brievely like that because I followed the steps and didn't manage to success.

我知道 github中的osmdroid was fixed recently但是.aar比修复版旧。在这里你可以找到 Resource Proxy class.

1 个答案:

答案 0 :(得分:1)

在MapView实例中创建时,需要传入对“ResourceProxy”实现的引用。默认情况下,它加载“DefaultResourceProxyImpl”。要覆盖,创建一个扩展DefaultResourceProxyImpl的新类,然后可以覆盖getBitmap和getDrawable。实际上有一个示例覆盖了DefaultResourceProxyImpl:

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

只为了你,我正在努力在osmdroid示例应用程序中包含这样的示例。它会看起来像这样

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

 }

编辑:完成 见https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/CustomResourceProxy.java

编辑:编辑: 维基更新,请参阅 https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library