先上效果图
点击小狗的Marker:
点击小猫的Marker:
首先,自定义Marker(实现图一):
//必须创建两个不同的 MarkerOptions 以及 Marker 用于区分身份类型
private MarkerOptions markerOptionsDog;
private MarkerOptions markerOptionsCat;
private Marker mMarkerDog;
private Marker mMarkerCat;
//MarkerOptions 要在地图初始化时new出来
private void initView() {
if (aMap == null) {
aMap = mMapView.getMap();
markerOptionsCat = new MarkerOptions();
markerOptionsDog = new MarkerOptions();
}
aMap.setOnMarkerClickListener(this);
}
//根据数据 判断是哪一种身份
switch (list.get(i).getType()) {
case 1:
//小狗
View viewDog = LayoutInflater.from(this).inflate(R.layout.marker_window_dog, null);
TextView textViewDog = (TextView) viewDog.findViewById(R.id.text_marker_window_dog);
textViewDog.setText(list.get(i).getDog());
/**
* 经度和纬度
*/
LatLng mLatlngDog = new LatLng(list.get(i).getLon(), list.get(i).getLat());
//将年龄和性别拼接起来 点击Marker后的窗口取出
String groupDog = list.get(i).getAge() + "#" + list.get(i).getGender();
markerOptionsDog.position(mLatlngDog)
.title(list.get(i).getDog())
.snippet(groupDog)
.draggable(false)
.icon(BitmapDescriptorFactory.fromView(viewDog));
//将数据添加到地图上
mMarkerDog = aMap.addMarker(markerOptionsDog);
//设置类型为1 为了区分 点击 哪一个类型的Marker
mMarkerDog.setObject(1);
break;
case 2:
//小猫
View viewCat = LayoutInflater.from(this).inflate(R.layout.marker_window_cat, null);
TextView textViewCat = (TextView) viewCat.findViewById(R.id.text_marker_window_cat);
textViewCat.setText(list.get(i).getCat());
/**
* 经度和纬度
*/
LatLng mLatlngCat = new LatLng(list.get(i).getLon(), list.get(i).getLat());
//将年龄和性别拼接起来
String groupCat = list.get(i).getAge() + "#" + list.get(i).getGender();
markerOptionsCat.position(mLatlngCat)
.title(list.get(i).getCat())
.snippet(groupCat)
.draggable(false)
.icon(BitmapDescriptorFactory.fromView(viewCat));
//将数据添加到地图上
mMarkerCat = aMap.addMarker(markerOptionsCat);
//设置类型为2 为了区分 点击 哪一个类型的Marker
mMarkerCat.setObject(2);
break;
default:
break;
}
实现图二、图三的自定义Window窗口:
//首先类要实现 OnMarkerClickListener 接口
public class CustomMarkerWindowActivity extends Activity implements AMap.OnMarkerClickListener
/**
* 对marker标注点 点击响应事件
*
* @param marker
* @return
*/
@Override
public boolean onMarkerClick(Marker marker) {
if (aMap != null & marker.getTitle() != null) {
// 点击地图Marker 打开Window
WindowDialog.getInstance().showDialog(marker, CustomMarkerWindowActivity.this);
}
return true;
}
//WindowDialog.getInstance().showDialog() 为一个自定义的Dialog
//将marker传入WindowDialog 类中
public void showDialog(Marker marker, Context mContext) {
//判断身份类型
switch (marker.getObject().toString()) {
case "1":
setDog(marker, mContext);
break;
case "2":
setCat(marker, mContext);
break;
default:
break;
}
}
//根据不同的身份展示不同的布局
/**
* 设置小狗的布局
* @param marker
* @param mContext
*/
private void setDog(Marker marker, Context mContext) {
View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_amap_marker_window_dog, null, false);
setAlertDialog(view);
TextView nameDog,ageDog,genderDog;
nameDog = (TextView) view.findViewById(R.id.text_name_marker_window_dog);
ageDog = (TextView) view.findViewById(R.id.text_age_marker_window_dog);
genderDog = (TextView) view.findViewById(R.id.text_gender_marker_window_dog);
//截取字段
String age = marker.getSnippet().substring(0, marker.getSnippet().indexOf("#"));
String gender = marker.getSnippet().replaceAll(age + "#", "");
nameDog.setText("名字:"+marker.getTitle());
ageDog.setText("年龄:"+age+"岁");
genderDog.setText("性别:"+gender+"的");
}
/**
* 设置小猫的布局
* @param marker
* @param mContext
*/
private void setCat(Marker marker, Context mContext) {
View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_amap_marker_window_cat, null, false);
setAlertDialog(view);
TextView nameCat,ageCat,genderCat;
nameCat = (TextView) view.findViewById(R.id.text_name_marker_window_cat);
ageCat = (TextView) view.findViewById(R.id.text_age_marker_window_cat);
genderCat = (TextView) view.findViewById(R.id.text_gender_marker_window_cat);
//截取字段
String age = marker.getSnippet().substring(0, marker.getSnippet().indexOf("#"));
String gender = marker.getSnippet().replaceAll(age + "#", "");
nameCat.setText("名字:"+marker.getTitle());
ageCat.setText("年龄:"+age+"岁");
genderCat.setText("性别:"+gender+"的");
}
由此全部实现Marker以及Window的自定义
Github:
https://github.com/RookieExaminer/AMap2DDemo
CustomMarkerWindowActivity.class 类