首先给出官方api上关于 marker和 infowindow的自定义方法:
aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
<span style="font-size:18px;">/**
* 监听自定义infowindow窗口的infocontents事件回调
*/
@Override
public View getInfoContents(Marker marker) {
if (radioOption.getCheckedRadioButtonId() != R.id.custom_info_contents) {
return null;
}
View infoContent = getLayoutInflater().inflate(
R.layout.custom_info_contents, null);
render(marker, infoContent);
return infoContent;
}
/**
* 监听自定义infowindow窗口的infowindow事件回调
*/
@Override
public View getInfoWindow(Marker marker) {
if (radioOption.getCheckedRadioButtonId() != R.id.custom_info_window) {
return null;
}
View infoWindow = getLayoutInflater().inflate(
R.layout.custom_info_window, null);
render(marker, infoWindow);
return infoWindow;
}
/**
* 自定义infowinfow窗口
*/
public void render(Marker marker, View view) {
if (radioOption.getCheckedRadioButtonId() == R.id.custom_info_contents) {
((ImageView) view.findViewById(R.id.badge))
.setImageResource(R.drawable.badge_sa);
} else if (radioOption.getCheckedRadioButtonId() == R.id.custom_info_window) {
ImageView imageView = (ImageView) view.findViewById(R.id.badge);
imageView.setImageResource(R.drawable.badge_wa);
}
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
if (title != null) {
SpannableString titleText = new SpannableString(title);
titleText.setSpan(new ForegroundColorSpan(Color.RED), 0,
titleText.length(), 0);
titleUi.setTextSize(15);
titleUi.setText(titleText);
} else {
titleUi.setText("");
}
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
if (snippet != null) {
SpannableString snippetText = new SpannableString(snippet);
snippetText.setSpan(new ForegroundColorSpan(Color.GREEN), 0,
snippetText.length(), 0);
snippetUi.setTextSize(20);
snippetUi.setText(snippetText);
} else {
snippetUi.setText("");
}
}</span>
需要做的工作,简单来讲,就是首先要给AMap设置Marker的点击监听 和infowindow自定义的回调监听;
然后在getInfoWindow 中加载你自定义的View对象,并返回;
API优先回调getInfoWindow ,如果返回的是null,会再回调getInfoContents;
这样设置完成后,点击marker,弹出的infowindow样式如下:
外围始终有一个高德自带的气泡框,关于如何替换这个气泡框,翻了一遍高德的api,完全没有一点点提示。
然而,万万没想到!!!我还是修改掉了这个气泡!
方法简直简单的令人发指!!!
在你自定义的view布局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:background="@android:color/transparent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/yellow"
android:orientation="horizontal"
android:gravity="center_vertical" >
<TextView
android:id="@+id/inforwindow_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/inforwindow_dest_desc_textsize" />
<Button
android:id="@+id/inforwindow_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center"
android:text="@string/inforwindow_btn_tips" />
</LinearLayout>
</LinearLayout>