安卓高德地圖之自定義infowindow

    所謂infowindow爲高德地圖中點擊marker出現的窗體,可根據需要自定義窗體的格式,並實現窗體功能添加。

    在此其他marker添加不贅述,在添加完marker之後,需要使用自定義infowindow的話得先添加監聽:

aMap.setOnMarkerClickListener(this);
		aMap.setInfoWindowAdapter(this);
    之後需要在marker點擊事件中顯示設置顯示infowindow,代碼如下:

/* marker點擊事件 */
	@Override
	public boolean onMarkerClick(Marker marker) {
		// TODO Auto-generated method stub
		marker.showInfoWindow(); // 顯示改點對應 的infowindow
		return false;
	}
    在getInfoWindow中進行infowindow的自定義,先給出代碼,結合代碼給出解釋:

/* 自定義窗體 */
	@Override
	public View getInfoWindow(final Marker marker) {
		// TODO Auto-generated method stub
		View infoWindow = getLayoutInflater().inflate(R.layout.display, null);//display爲自定義layout文件
		TextView name = (TextView) infoWindow.findViewById(R.id.name);
		name.setText("景點名稱:" + marker.getTitle());
		LatLng l = marker.getPosition();// 獲取標籤的位置
		TextView dis = (TextView) infoWindow.findViewById(R.id.dis);
		float distance = AMapUtils.calculateLineDistance(l, la) / 1000;// 調用函數計算距離
		description = "距您所在位置:" + distance + "KM" + "\n";
		dis.setText(description);
		TextView des = (TextView) infoWindow.findViewById(R.id.des);
		des.setText("景點簡介:" + marker.getSnippet());
		//此處省去長篇代碼
		return infoWindow;
	}
    上述代碼先聲明一個view變量,後加載自定義的infowindow窗體,需要注意的是給layout中的窗體控件賦值的時候千萬注意需要用infowindow.findViewById方法,前面的infowindow爲定義的infowindow變量,這一點萬不可弄錯,否則無法顯示。

    另外,添加的marker需要設置title,否則無法顯示自定義title。至此,自定義infowindow設置完成。

    如果不想使用高德自帶的背景色的話,在自定義佈局文件中,添加如下代碼即可自定義背景顏色:

 style="background-color: rgb(255, 255, 102);"   android:background="@android:color/transparent" 
    效果圖:


    特記下,以備後日回顧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章