国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 百度地圖marker中圖片不顯示的解決方法(推薦)

2019-12-12 01:13:38
字體:
供稿:網(wǎng)友

目的:

根據(jù)提供的多個(gè)經(jīng)緯度,顯示所在地的marker樣式,如下:

問題:

1.發(fā)現(xiàn)marker中在線加載的圖片無法顯示出來;

2.獲取多個(gè)對(duì)象后,卻只顯示出了一個(gè)marker;

以下為官網(wǎng)實(shí)現(xiàn)方法:

通過查閱百度官網(wǎng)的文檔,我們可以知道,地圖標(biāo)注物的實(shí)現(xiàn)方法如下:

//定義Maker坐標(biāo)點(diǎn) 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對(duì)象即可,獲取BitmapDescriptor的方式有如下幾種:

(查詢地址:http://wiki.lbsyun.baidu.com/cms/androidsdk/doc/v4_3_0/index.html

因?yàn)槭莔arker不是簡單的圖片展示,所以這里是需要自定義view給加載進(jìn)來的,因此使用的是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>

但是如果此時(shí)使用fromView來加載視圖生成BitmapDescriptor就會(huì)導(dǎo)致:在圖片未在線加載完畢時(shí),整個(gè)view就已經(jīng)生成了BitmapDescriptor,這個(gè)時(shí)候就出現(xiàn)了marker無法顯示圖片的問題(這就好比經(jīng)典招數(shù)“猴子偷桃”,桃都還沒有,怎么偷得出來呢)。所以解決的辦法是――>等到圖片加載完畢后再去生成BitmapDescriptor,從而設(shè)置MarkerOptions。

下面為我的解決方案:

以下是在fragment中加載以上的視圖文件:(具體業(yè)務(wù)情況看個(gè)人的頁面設(shè)計(jì),所以使用的時(shí)候也不一定非得在這個(gè)方法里面初始化)

@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,否則此處會(huì)報(bào)錯(cuò)加載的對(duì)象不是要求的布局對(duì)象      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;  } 

因?yàn)樯婕暗蕉鄠€(gè)marker的加載,意味著要對(duì)布局進(jìn)行多次加載,所以此處使用ViewHolder來處理重復(fù)的操作。

然后進(jìn)行初始化各個(gè)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從地上生長出來的動(dòng)畫      Marker marker = (Marker) baiduMap.addOverlay(overlayOptions);      Bundle bundle = new Bundle();      bundle.putSerializable(ConstantValues.User.GEO_USER_ENTITY, geoUserEntityTemp);      marker.setExtraInfo(bundle);//marker點(diǎn)擊事件監(jiān)聽時(shí),可以獲取到此時(shí)設(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方法來進(jìn)行異步圖片加載,然后用自定義的ResultListener接口來回調(diào)圖片加載完畢的結(jié)果,從而初始化BitmapDescriptor,進(jìn)而設(shè)置MarkerOptions。

要點(diǎn):

GlideCircleTransform是自定義的類,用來設(shè)置圖片為圓形(之后有實(shí)現(xiàn)方法);

onResourceReady是Glide框架中監(jiān)聽圖片加載完畢的回調(diào)方法,以上的實(shí)現(xiàn)能監(jiān)聽多張圖片加載完畢的事件;

resultListener.onReturnResult(markerView);中的markerView是一開始自定義的marker中要加載的整個(gè)布局;

為啥不用SimpleDraweeView來實(shí)現(xiàn)圓形圖片并進(jìn)行圖片下載的監(jiān)聽?

僅個(gè)人的使用情況:使用Glide框架無法控制SimpleDraweeView的形狀;而且,SimpleDraweeView無法監(jiān)聽動(dòng)態(tài)加載同一個(gè)view時(shí)的多張圖片下載的情況,意味著只能監(jiān)聽到最后最后一張圖片。(也就是,類似于上面的onReturnResult方法只會(huì)回調(diào)一次,這也就是本文開頭提到過的問題②:獲取多個(gè)對(duì)象后,卻只顯示出了一個(gè)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();  }}

在對(duì)未知進(jìn)行探索時(shí),一點(diǎn)是興奮的,兩點(diǎn)是開心的,三點(diǎn)是積極的,太多太多的點(diǎn)則是痛苦的,而解決了迷惑之后內(nèi)心是舒坦的!

以上這篇Android 百度地圖marker中圖片不顯示的解決方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀柔区| 南江县| 钦州市| 霍州市| 布拖县| 富蕴县| 称多县| 白沙| 施秉县| 拜城县| 平利县| 布拖县| 武义县| 新宾| 启东市| 龙陵县| 抚松县| 大同县| 兴山县| 蓬安县| 红原县| 尚义县| 定日县| 将乐县| 高淳县| 霍林郭勒市| 原阳县| 宁德市| 临沂市| 华阴市| 昌邑市| 都江堰市| 额尔古纳市| 九江市| 长宁县| 九江县| 彭水| 盐城市| 镇巴县| 鲜城| 汝南县|