android高德自定义图标,Android 高德地图显示在线图标
項目剛好用到高德地圖,需求里有要求顯示在線圖標,發現高德Demo里只有顯示本地圖標的的例子,那么網絡配置的圖標要怎么顯示呢,這時候可能就想到了自定義View,因為高德地圖支持自定義View作為Marker,但是我們會發現使用自定義View,然后在里面利用框架顯示圖片,還是不能正確展示,可能是空白的,這是因為Android網絡請求是異步的,而高德地圖為覆蓋物設置自定義view其本質也是將View的視圖轉成圖片,我們將自定View設置給Marker時,圖片可能正在加載過程中,這個時候將View轉成了圖片,其實網絡圖片還沒有加載到視圖上,所以我們就看不到添加的marker了。那么怎樣才能實現我們的需求呢,其實很簡單將網絡圖片下載下來,再設置給Marker就可以了。我這邊使用的是Glide來顯示網絡圖片,首先我們需要先將圖片下載下載
/**
* 設置網絡圖片
*
* @param imageBean 圖片對象,包含經緯度、圖片鏈接
*/
public void setGeniusIcon(final ImageBean imageBean) {
Glide.with(CtxHelper.getApp())
.load(bike.getBikeLogo() == null ? "" : imageBean.getLogo())
.asBitmap()
.into(new SimpleTarget() {
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
//圖片加載失敗顯示本地默認圖標
Bitmap newBmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(CtxHelper.getApp().getResources(), R.drawable.homepage_qeebike_icon), DisplayUtil.dip2px(CtxHelper.getApp(), 50f), DisplayUtil.dip2px(CtxHelper.getApp(), 50f), true);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(newBmp);
addMarker(bike, bitmapDescriptor);
}
@Override
public void onResourceReady(Bitmap resource, GlideAnimation super Bitmap> glideAnimation) {
//圖標下載成功重新生成自定義大小的Bitmap
Bitmap newBmp = Bitmap.createScaledBitmap(resource, DisplayUtil.dip2px(CtxHelper.getApp(), 50f), DisplayUtil.dip2px(CtxHelper.getApp(), 50f), true);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(newBmp);
//添加覆蓋物
addMarker(bike, bitmapDescriptor);
}
});
}
/**
* 添加覆蓋物
*
* @param bike
* @param bitmapDescriptor
*/
private void addMarker(ImageBean imageBean, BitmapDescriptor bitmapDescriptor) {
LatLng latLng = new LatLng(imageBean.getLatitude(), imageBean.getLongitude());
mLatLngList.add(new LatLng(imageBean.getLatitude(), imageBean.getLongitude()));
MarkerOptions markerOption = new MarkerOptions()
.icon(bitmapDescriptor)
.position(latLng)
// .anchor(0.5f,1.0f)
// .title("離我最近")
//.setInfoWindowOffset(DisplayUtil.dip2px(CtxHelper.getApp(),15),DisplayUtil.dip2px(CtxHelper.getApp(),13))
.snippet(null)
.draggable(true);
Marker marker = mAMap.addMarker(markerOption);
marker.setObject(bike);
if (i == 0) {
marker.showInfoWindow();
}
i++;
Animation animation = new ScaleAnimation(0, 1, 0, 1);
animation.setInterpolator(new LinearInterpolator());
//整個動畫所需要的時間
animation.setDuration(300);
//設置動畫
marker.setAnimation(animation);
mMarkerList.add(marker);
//開始動畫
marker.startAnimation();
}
調用
在獲取到覆蓋物列表信息后依次添加覆蓋物
for (final ImageBean imageBean : imageBeans) {
setGeniusIcon(imageBean);
}
到這里我們就可以正確顯示網絡圖片了,以上是glide下載圖片,使用其他框架的可自行替換掉下載那一塊的代碼就可以了。
總結
以上是生活随笔為你收集整理的android高德自定义图标,Android 高德地图显示在线图标的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言 文件序列化,Obj-C中的NSD
- 下一篇: android怎么实现标题搜索,Andr