一开始我解决这个问题的思路是先百度看看有没有现成的拿来用,找半天没找到,最后自己在看百度地图的api时发现了,百度地图提供的那个实例化方法,问题自然就迎刃而解了。建议大家以后解决问题时先去研究官方的API,实在不行再去搜索大牛的博客来学习,不要一遇到问题不想就去百度找答案。这不是好程序员要做的事情,自己多研究才能与时俱进,便于以后自己去学习新技术!
废话到此,先上效果图:(下图,检索到的加油站上方还有一行文本简单描述了加油站的信息)

自定义MarkerOptions的布局样式关键在于创建他的icon是自定义实现的,也就是说要自定义BitmapDescriptor。
BitmapDescriptor bd_temp;
MarkerOptions oo = new MarkerOptions().position(ll1)
.icon(bd_temp)
.anchor(0.5f, 1.0f).zIndex(7);
自定义BitmapDescriptor,就要用到他其中之一的工厂方法来实例化一个BitmapDescriptor,也就是:
View v_temp = LayoutInflater.from(getApplicationContext()).inflate(R.layout.text_up_img_down, null);//加载自定义的布局
TextView tv_temp = (TextView) v_temp.findViewById(R.id.baidumap_custom_text);//获取自定义布局中的textview
ImageView img_temp = (ImageView) v_temp.findViewById(R.id.baidumap_custom_img);//获取自定义布局中的imageview
tv_temp.setText(name);//设置要显示的文本
img_temp.setImageResource(imgIds[i]);//设置marker的图标
bd_temp = BitmapDescriptorFactory.fromView(v_temp);//用到了这个实例化方法来把自定义布局实现到marker中。
至此就可以将自定义的布局加载到marker中了
mBaiduMap.addOverlay(oo);
自定义布局文件text_up_img_down.xml(文字在上图标在下):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/baidumap_custom_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="这是文本在上"
android:textSize="15sp"
/>
<ImageView
android:id="@+id/baidumap_custom_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_gcoding"/>
</LinearLayout>
效果图:
本文介绍如何使用百度地图API自定义Marker图标,并展示了一个具体的实现案例。通过自定义BitmapDescriptor,实现了文字和图标上下排列的Marker布局。


295

被折叠的 条评论
为什么被折叠?



