在使用百度地图的PoiOverlay的时候,
调用语句是:
PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
poiOverlay.setData(poiResult);
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
出现了marker’s icon can not be null的报错信息如下图:
跳转进报错语句的位置后,发现错误定位的代码如下:
我尝试了,将要求的Icon_markxxx.png拷贝进assets文件夹下,或者拷贝进drawable文件夹下,还是报错
我也查不出来这些.png图片放在哪里才能被成功找到。
最后,找到了一个解决办法,那就是改这个实现方法getOverlayOptions()
将
markerList.add(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromAssetWithDpi("Icon_mark" + markerSize + ".png"))
.extraInfo(bundle)
.position(mPoiResult.getAllPoi().get(i).location))
改写成
markerList.add(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.xxx))//R.drawable.xxx是需要改变的,这里只是简单的写成一个图片示意一下
.extraInfo(bundle)
.position(mPoiResult.getAllPoi().get(i).location));
然后再将要显示的.png图片,复制到drawable文件夹下,如下图
更改后的代码如下:
//在PoiOverlay.class中直接修改方法public final List<OverlayOptions> getOverlayOptions(){}
//百度地图原来提供的图标命名是icon_marka/b/c/d.png我自己改成了icon_mark1/2/3/4.png,方便更改icon值而已
@Override
public final List<OverlayOptions> getOverlayOptions() {
if (mPoiResult == null || mPoiResult.getAllPoi() == null) {
return null;
}
List<OverlayOptions> markerList = new ArrayList<>();
int index = 0;
for (int i = 0; i < mPoiResult.getAllPoi().size() && index < MAX_POI_SIZE; i++) {
if (mPoiResult.getAllPoi().get(i).location == null) {
continue;
}
String iconIndex = ++index + "";
int icon = getResId("icon_mark" + iconIndex, R.drawable.class);
Bundle bundle = new Bundle();
bundle.putInt("index", i);
markerList.add(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(icon))
.extraInfo(bundle)
.position(mPoiResult.getAllPoi().get(i).location));
}
return markerList;
}
private static int getResId(String variableName, Class<?> c) {
try {
Field idField = c.getDeclaredField(variableName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
其实不管是PoiOverlay还是BusLineOverlay,出现marker’s icon can not be null报错,定位到的错误都是fromAssetWithDpi,那么更改方法都是一样的,将fromAssetWithDpi改成fromResource就可以了(记住图标放在drawable文件夹)。
但是,这样改会不会有什么更大的隐患我就不知道了,可以正常工作我就知足了23333~~~