gpt4 book ai didi

android - LayerList 作为 Google map v2 中标记上的图标

转载 作者:太空狗 更新时间:2023-10-29 16:41:24 27 4
gpt4 key购买 nike

我有以下 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());
}

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

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)

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

最佳答案

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

第一步:将可绘制对象转换为位图:

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

它在我的案例中是活跃的。

关于android - LayerList 作为 Google map v2 中标记上的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17138998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com