Android 百度地圖marker中圖片不顯示的解決方法(推薦)
目的:
根據(jù)提供的多個經(jīng)緯度,顯示所在地的marker樣式,如下:

問題:
1.發(fā)現(xiàn)marker中在線加載的圖片無法顯示出來;
2.獲取多個對象后,卻只顯示出了一個marker;
以下為官網(wǎng)實現(xiàn)方法:
通過查閱百度官網(wǎng)的文檔,我們可以知道,地圖標(biāo)注物的實現(xiàn)方法如下:
//定義Maker坐標(biāo)點 LatLng point = new LatLng(39.963175, 116.400244); //構(gòu)建Marker圖標(biāo) BitmapDescriptor bitmap = BitmapDescriptorFactory .fromResource(R.drawable.icon_marka); //構(gòu)建MarkerOption,用于在地圖上添加Marker OverlayOptions option = new MarkerOptions() .position(point) .icon(bitmap); //在地圖上添加Marker,并顯示 mBaiduMap.addOverlay(option);
那么想通過更改marker的樣式,也就是option中的.icon(BitmapDescriptor)方法,只需要提供BitmapDescriptor對象即可,獲取BitmapDescriptor的方式有如下幾種:

(查詢地址:http://wiki.lbsyun.baidu.com/cms/androidsdk/doc/v4_3_0/index.html)
因為是marker不是簡單的圖片展示,所以這里是需要自定義view給加載進來的,因此使用的是fromView()來加載自定義視圖,視圖內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:id="@+id/bitmapFl"
android:layout_height="44dp">
<ImageView
android:layout_width="30dp"
android:layout_height="44dp"
android:layout_gravity="center"
android:src="@mipmap/anchor"/>
<ImageView
android:id="@+id/picSdv"
android:layout_marginTop="1dp"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_gravity="center_horizontal"
/>
</FrameLayout>
但是如果此時使用fromView來加載視圖生成BitmapDescriptor就會導(dǎo)致:在圖片未在線加載完畢時,整個view就已經(jīng)生成了BitmapDescriptor,這個時候就出現(xiàn)了marker無法顯示圖片的問題(這就好比經(jīng)典招數(shù)“猴子偷桃”,桃都還沒有,怎么偷得出來呢)。所以解決的辦法是——>等到圖片加載完畢后再去生成BitmapDescriptor,從而設(shè)置MarkerOptions。
下面為我的解決方案:
以下是在fragment中加載以上的視圖文件:(具體業(yè)務(wù)情況看個人的頁面設(shè)計,所以使用的時候也不一定非得在這個方法里面初始化)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
this.inflater = inflater;
this.container = container;
if (markerView == null) {
viewHolder = new ViewHolder();
markerView = (FrameLayout) inflater.inflate(R.layout.layout_bitmap_descriptor, container, true).findViewById(R.id.bitmapFl);//此處一定要find到bitmapFl,否則此處會報錯加載的對象不是要求的布局對象
viewHolder.imageView = (ImageView) markerView.findViewById(R.id.picSdv);
markerView.setTag(viewHolder);
}
return inflater.inflate(R.layout.fragment_main, container, false);
}
ivate class ViewHolder {
ImageView imageView;
}
因為涉及到多個marker的加載,意味著要對布局進行多次加載,所以此處使用ViewHolder來處理重復(fù)的操作。
然后進行初始化各個marker:
/**
* 初始化位置標(biāo)注信息
*
* @param geoUserEntities:地理用戶數(shù)據(jù)(包含頭像地址、經(jīng)緯度)
*/
private void initOverlayOptions(List<GeoUserEntity> geoUserEntities) {
baiduMap.clear();
AvatarEntity avatarEntityTemp;
for (int i = 0; i < geoUserEntities.size(); i++) {
if (geoUserEntities.get(i) == null) {
continue;
}
final Marker[] marker = {null};
final GeoUserEntity geoUserEntityTemp = geoUserEntities.get(i);
avatarEntityTemp = geoUserEntityTemp.getAvatar();
final BitmapDescriptor[] pic = {null};
if (avatarEntityTemp != null && !StringUtils.isEmpty(avatarEntityTemp.getImageUrl())) {
returnPicView(avatarEntityTemp.getMid(), new ResultListener() {//圖片加載完畢后的回調(diào)方法
@Override
public void onReturnResult(Object object) {
super.onReturnResult(object);
pic[0] = BitmapDescriptorFactory.fromView((View) object);
putDataToMarkerOptions(pic[0], geoUserEntityTemp);
}
});
} else if (avatarEntityTemp == null) {
pic[0] = BitmapDescriptorFactory.fromResource(R.mipmap.anchor);
putDataToMarkerOptions(pic[0], geoUserEntityTemp);
}
}
}
/**
* 顯示marker,設(shè)置marker傳遞的數(shù)據(jù)
*
* @param pic
* @param geoUserEntityTemp
*/
private void putDataToMarkerOptions(BitmapDescriptor pic, GeoUserEntity geoUserEntityTemp) {
double[] locationTemp = geoUserEntityTemp.getLocation();
if (locationTemp != null && locationTemp.length == 2) {
LatLng point = new LatLng(locationTemp[1], locationTemp[0]);
MarkerOptions overlayOptions = new MarkerOptions()
.position(point)
.icon(pic)
.animateType(MarkerOptions.MarkerAnimateType.grow);//設(shè)置marker從地上生長出來的動畫
Marker marker = (Marker) baiduMap.addOverlay(overlayOptions);
Bundle bundle = new Bundle();
bundle.putSerializable(ConstantValues.User.GEO_USER_ENTITY, geoUserEntityTemp);
marker.setExtraInfo(bundle);//marker點擊事件監(jiān)聽時,可以獲取到此時設(shè)置的數(shù)據(jù)
marker.setToTop();
}
}
private void returnPicView(String urlTemp, final ResultListener resultListener) {
viewHolder = (ViewHolder) markerView.getTag();
Glide.with(getContext())
.load(urlTemp)
.asBitmap()
.transform(new GlideCircleTransform(getContext()))
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
viewHolder.imageView.setImageBitmap(bitmap);
resultListener.onReturnResult(markerView);
}
}
);
}
通過returnPicView方法來進行異步圖片加載,然后用自定義的ResultListener接口來回調(diào)圖片加載完畢的結(jié)果,從而初始化BitmapDescriptor,進而設(shè)置MarkerOptions。
要點:
GlideCircleTransform是自定義的類,用來設(shè)置圖片為圓形(之后有實現(xiàn)方法);
onResourceReady是Glide框架中監(jiān)聽圖片加載完畢的回調(diào)方法,以上的實現(xiàn)能監(jiān)聽多張圖片加載完畢的事件;
resultListener.onReturnResult(markerView);中的markerView是一開始自定義的marker中要加載的整個布局;
為啥不用SimpleDraweeView來實現(xiàn)圓形圖片并進行圖片下載的監(jiān)聽?
僅個人的使用情況:使用Glide框架無法控制SimpleDraweeView的形狀;而且,SimpleDraweeView無法監(jiān)聽動態(tài)加載同一個view時的多張圖片下載的情況,意味著只能監(jiān)聽到最后最后一張圖片。(也就是,類似于上面的onReturnResult方法只會回調(diào)一次,這也就是本文開頭提到過的問題②:獲取多個對象后,卻只顯示出了一個marker的情況)
然后,附上GlideCircleTransform的類代碼(從其它處拷貝過來的):
public class GlideCircleTransform extends BitmapTransformation {
public Context context;
public GlideCircleTransform(Context context) {
super(context);
this.context = context;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}
在對未知進行探索時,一點是興奮的,兩點是開心的,三點是積極的,太多太多的點則是痛苦的,而解決了迷惑之后內(nèi)心是舒坦的!
以上這篇Android 百度地圖marker中圖片不顯示的解決方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android百度地圖添加Marker失真問題的解決方案
- 關(guān)于Android高德地圖的簡單開發(fā)實例代碼(DEMO)
- Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
- Android開發(fā)之高德地圖實現(xiàn)定位
- Android之高德地圖定位SDK集成及地圖功能實現(xiàn)
- Android仿微信調(diào)用第三方地圖應(yīng)用導(dǎo)航(高德、百度、騰訊)
- Android 高德地圖之poi搜索功能的實現(xiàn)代碼
- GMap.Net開發(fā)之自定義Marker使用方法
- Android基于高德地圖完全自定義Marker的實現(xiàn)方法
相關(guān)文章
Android實現(xiàn)調(diào)用攝像頭拍照與視頻功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)調(diào)用攝像頭拍照與視頻功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
這篇文章主要為大家詳細介紹了Android中ToggleButton開關(guān)狀態(tài)按鈕控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android編程實現(xiàn)Dialog窗體監(jiān)聽的方法
這篇文章主要介紹了Android編程實現(xiàn)Dialog窗體監(jiān)聽的方法,結(jié)合實例形式分析了Android針對Dialog對話框窗體事件監(jiān)聽與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android實現(xiàn)錄音功能實現(xiàn)實例(MediaRecorder)
本篇文章主要介紹了Android實現(xiàn)錄音的實例代碼(MediaRecorder),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
淺析Android Service中實現(xiàn)彈出對話框的坑
這篇文章主要介紹了Android Service中實現(xiàn)彈出對話框的坑,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
面向?qū)ο缶幊掏ㄟ^對事物的抽象,大大的簡化了程序的開發(fā)難度。我們常用的編程語言:Java、C++、Python都屬于面向?qū)ο缶幊?。Kotlin與java類似,也是一種面向?qū)ο缶幊陶Z言。本文從面向?qū)ο笕齻€基本特征:封裝、繼承、多態(tài),來闡述一下Kotlin中的面向?qū)ο缶幊?/div> 2022-12-12
Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個 Android 項目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來與大家分享2013-01-01最新評論

