3

我有以下 LayerList 对象

R.drawable.flagged_pin

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pin_marker" />
    <item android:drawable="@drawable/flag" />
</layer-list>

我希望用作标记。

我从我的 POJO 方法返回一个 MarkerOptions

 public class Store {
     protected String name;
     protected LatLng latLng;
     protected String address;

     // ....

     public MarkerOptions getMarkerOptions() {
         MarkerOptions markerOptions = new MarkerOptions();
         markerOptions.position(this.latLng);
         markerOptions.title(this.name);
         markerOptions.snippet(this.address);

         int res = R.drawable.flagged_pin;
         markerOptions.icon(BitmapDescriptorFactory.fromResource(res));

         return markerOptions;
     }

}

在 MapFragment 中

for (Store store : stores) {
    marker = getMap().addMarker(store.getMarkerOptions());
}

我没有结果:地图上没有绘制标记上的图标。

StackTrace 是

E/StoresMapFragment_(29590): A runtime exception was thrown while executing code in a runnable
E/StoresMapFragment_(29590): java.lang.NullPointerException
E/StoresMapFragment_(29590):    at maps.aj.bk.a(Unknown Source)
E/StoresMapFragment_(29590):    at maps.aj.bk.a(Unknown Source)
E/StoresMapFragment_(29590):    at maps.aj.al.a(Unknown Source)
E/StoresMapFragment_(29590):    at bcn.onTransact(SourceFile:167)
E/StoresMapFragment_(29590):    at android.os.Binder.transact(Binder.java:326)
E/StoresMapFragment_(29590):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
E/StoresMapFragment_(29590):    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
E/StoresMapFragment_(29590):    at cl.paonde.fragment.map.StoresMapFragment.placeStores(StoresMapFragment.java:121)
E/StoresMapFragment_(29590):    at cl.paonde.fragment.map.StoresMapFragment_.access$0(StoresMapFragment_.java:1)
E/StoresMapFragment_(29590):    at cl.paonde.fragment.map.StoresMapFragment_$1.run(StoresMapFragment_.java:65)
E/StoresMapFragment_(29590):    at android.os.Handler.handleCallback(Handler.java:615)
E/StoresMapFragment_(29590):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/StoresMapFragment_(29590):    at android.os.Looper.loop(Looper.java:137)
E/StoresMapFragment_(29590):    at android.app.ActivityThread.main(ActivityThread.java:4898)
E/StoresMapFragment_(29590):    at java.lang.reflect.Method.invokeNative(Native Method)
E/StoresMapFragment_(29590):    at java.lang.reflect.Method.invoke(Method.java:511)
E/StoresMapFragment_(29590):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/StoresMapFragment_(29590):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/StoresMapFragment_(29590):    at dalvik.system.NativeStart.main(Native Method)

我的问题:我做错了什么?,是否可以将 LayerList 作为图标而不在我的 POJO 中使用上下文?

4

1 回答 1

7

我有同样的问题。我帮助了这个答案 How to convert a Drawable to a Bitmap?

第一步:将drawable转换为位图:

    public static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

第二步,在标记中设置位图

    Drawable drawable = getResources().getDrawable(R.drawable.markerlayer);
    Bitmap converBtm = drawableToBitmap (drawable);
    BitmapDescriptor drawableMarker = BitmapDescriptorFactory
                    .fromBitmap(converBtm);
    MarkerOptions markerOpt = new MarkerOptions();
    markerOpt.icon(drawableMarker);

在我的情况下它是活跃的。

于 2013-09-15T12:00:22.087 回答