想在集成的高德地图中实现一个marker受到点击时,图标变大的效果,网上查了好多,要么不符合我的需求,要么语焉不详,干脆自己动手写了个,现在把代码分享出来
aMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();//显示大图标
lastBm = getNormalIcon(marker.getOptions());//记录即将被覆盖的原图,用于以后恢复
Marker markerNew = changeMarkerImg(marker);//改变marker的icon,并得到新的marker对象
String curMarkerId = markerNew.getId();// 记录下这个图标的id,用于判断再次点击是否还是这个按钮
resetMarker(curMarkerId, markerNew);//重置除点击按钮外的所有按钮
markerLast = markerNew;// 记录当前marker对象,用于下次点击时判断
return true;
}
});
2,更换marker的icon方法
private Marker changeMarkerImg(Marker marker) {
MarkerOptions options = marker.getOptions();
options.getIcon().recycle();// 回收原图片
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ico_dot);
marker.setIcon(BitmapDescriptorFactory.fromBitmap(bm));
return marker;
}
3,重置除点击按钮外的所有按钮
private void resetMarker(String curMarkerId, Marker markerNew) {
if (null != markerLast) {
if (!TextUtils.equals(curMarkerId, markerLast.getId())) {
markerLast.setIcon(new BitmapDescriptorFactory()
.fromBitmap(lastBm));
}
}
}
4,生成infoWindow的回调方法
@Override
public View getInfoWindow(Marker marker) {
View infoWindow = getPressView(marker);
return infoWindow;
}
@SuppressLint("NewApi")
public View getPressView(Marker marker) {
// 小气泡部分
View infoWindow = getLayoutInflater().inflate(
R.layout.custom_info_window, null);
Bitmap bitmap = getPressIcon(marker.getOptions());
BitmapDrawable background = new BitmapDrawable(
MarkerActivity.this.getResources(), bitmap);
infoWindow.findViewById(R.id.info_window).setBackground(background);
// 业务部分
buttomView.setVisibility(View.VISIBLE);
marker.getSnippet();
if (marker.getSnippet().contains("加油站")) {
// 加油站图片
markerName.setText("加油站");
} else if (marker.getSnippet().contains("银行")) {
// 银行图片
markerName.setText("银行");
} else if (marker.getSnippet().contains("厕所")) {
// 厕所图片
markerName.setText("厕所");
}
markerContent.setText("" + marker.getTitle());
return infoWindow;
}
5,生成默认图标bitmap的方法
private Bitmap getNormalIcon(MarkerOptions options) {
Bitmap bm = null;
if (options.getSnippet().contains("加油站")) {
// 加油站图片
bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ico_refuel_normal);
} else if (options.getSnippet().contains("银行")) {
// 银行图片
bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ico_money_normal);
} else if (options.getSnippet().contains("厕所")) {
// 厕所图片
bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ico_toilet_normal);
}
return bm;
}