編輯:關於 Android 程式設計
本文示範如何在百度地圖上動態生成覆蓋物、為每個覆蓋物綁定商家資料,並在點擊時彈出自訂的資訊窗與詳情面板。
需求是這樣的:使用者點擊「衣食住行」或進行周邊搜尋,伺服器回傳經緯度與商家資訊,App 據此動態生成覆蓋物,點擊後顯示詳情。
關於圖片:手機記憶體有限,所有網路圖片都應該走快取(如 LruCache),命中就直接取,未命中才下載並寫回,讓圖片佔用的記憶體永不超過設定上限。本篇重點不在此,因此直接放了幾張本地圖片來模擬。
伺服器回傳的 JSON 需轉成實體集合。這裡用靜態清單模擬:
public class Info implements Serializable {
private static final long serialVersionUID = -758459502806858414L;
private double latitude; // 緯度
private double longitude; // 經度
private int imgId; // 圖片 ID(真實專案可能是 URL)
private String name; // 商家名稱
String distance; // 距離
private int zan; // 讚數量
public static List<Info> infos = new ArrayList<Info>();
static {
infos.add(new Info(34.242652, 108.971171, R.drawable.a01,
"英倫貴族小旅館", "距離209米", 1456));
infos.add(new Info(34.242952, 108.972171, R.drawable.a02,
"沙井國際洗浴會所", "距離897米", 456));
infos.add(new Info(34.242852, 108.973171, R.drawable.a03,
"五環服裝城", "距離249米", 1456));
infos.add(new Info(34.242152, 108.971971, R.drawable.a04,
"老米家泡饃小炒", "距離679米", 1456));
}
// getter / setter 省略
}
為了方便,按鈕放在 OptionsMenu 裡:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.id_menu_map_addMaker:
addInfosOverlay(Info.infos);
break;
}
return true;
}
/** 初始化圖層 */
public void addInfosOverlay(List<Info> infos) {
mBaiduMap.clear();
LatLng latLng = null;
for (Info info : infos) {
latLng = new LatLng(info.getLatitude(), info.getLongitude());
OverlayOptions overlayOptions = new MarkerOptions()
.position(latLng)
.icon(mIconMaker)
.zIndex(5);
Marker marker = (Marker) mBaiduMap.addOverlay(overlayOptions);
// 把商家資料掛在 Marker 上,點擊時再取出
Bundle bundle = new Bundle();
bundle.putSerializable("info", info);
marker.setExtraInfo(bundle);
}
// 把地圖移到最後一個座標
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latLng));
}
重點是 setExtraInfo():Marker 本身只能表達位置與圖示,但我們可以把任意 Bundle 掛上去,讓後續點擊事件能取回對應的業務資料。這是實作「覆蓋物與資料關聯」最乾淨的做法。
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
Info info = (Info) marker.getExtraInfo().get("info");
// 建立要在地圖上顯示的 InfoWindow 內容
TextView location = new TextView(getApplicationContext());
location.setBackgroundResource(R.drawable.location_tips);
// 注意順序:先設背景,再設 padding,否則 padding 可能失效
location.setPadding(30, 20, 30, 50);
location.setText(info.getName());
// 把經緯度轉成螢幕座標,往上偏移避免遮住圖示
final LatLng ll = marker.getPosition();
Point p = mBaiduMap.getProjection().toScreenLocation(ll);
p.y -= 47;
LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);
mInfoWindow = new InfoWindow(location, llInfo,
new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
mBaiduMap.hideInfoWindow();
}
});
mBaiduMap.showInfoWindow(mInfoWindow);
mMarkerInfoLy.setVisibility(View.VISIBLE);
popupInfo(mMarkerInfoLy, info);
return true;
}
});
用 ViewHolder 避免重複 findViewById:
private class ViewHolder {
ImageView infoImg;
TextView infoName;
TextView infoDistance;
TextView infoZan;
}
protected void popupInfo(RelativeLayout mMarkerLy, Info info) {
ViewHolder viewHolder;
if (mMarkerLy.getTag() == null) {
viewHolder = new ViewHolder();
viewHolder.infoImg = (ImageView) mMarkerLy.findViewById(R.id.info_img);
viewHolder.infoName = (TextView) mMarkerLy.findViewById(R.id.info_name);
viewHolder.infoDistance = (TextView) mMarkerLy.findViewById(R.id.info_distance);
viewHolder.infoZan = (TextView) mMarkerLy.findViewById(R.id.info_zan);
mMarkerLy.setTag(viewHolder);
}
viewHolder = (ViewHolder) mMarkerLy.getTag();
viewHolder.infoImg.setImageResource(info.getImgId());
viewHolder.infoName.setText(info.getName());
viewHolder.infoDistance.setText(info.getDistance());
viewHolder.infoZan.setText(info.getZan() + "");
}
最後補上地圖空白處的單擊事件,用來收起詳情面板與資訊窗:
mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng arg0) {
mMarkerInfoLy.setVisibility(View.GONE);
mBaiduMap.hideInfoWindow();
}
});
除了 MapView,其餘都是詳情面板,預設隱藏:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baidu.mapapi.map.MapView
android:id="@+id/id_bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<RelativeLayout
android:id="@+id/id_marker_info"
android:layout_width="fill_parent"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:background="#CC4e5a6b"
android:clickable="true"
android:visibility="gone">
<ImageView
android:id="@+id/info_img"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_margin="10dp"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/info_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF5EB" />
<TextView
android:id="@+id/info_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF5EB" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="zan"
android:src="@drawable/map_zan" />
<TextView
android:id="@+id/info_zan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF5EB" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
mBaiduMap.clear() 並 dispose MapView,否則容易記憶體洩漏。
Java/Android 設計模式:抽象工廠模式解析與實作
抽象工廠模式是創建型設計模式之一,適合用於建立一系列相關或互相依賴的物件。當系統需要同時產生多種產品,而且這些產品之間存在關聯時,抽象工廠能讓程式碼更清楚、更易擴充。
wipe 是什麼意思?安卓手機 wipe 與雙 wipe 完整說明
剛接觸安卓刷機的人,看到「雙 wipe 再刷機」這種說法常常一頭霧水。到底 wipe 是什麼、為什麼要 wipe、怎麼 wipe?下面把它講清楚。 一、wipe 是什麼
Android 編程實現向桌面添加快捷方式的兩種方法
本文講解 Android 編程實現向桌面添加快捷方式的兩種方法,分享給大家參考。 有時候為了使用方便,需要在桌面上添加快捷方式,下面介紹兩種做法。 方法一 void s
仿 Win8 Metro 風格 UI 介面(上):自訂縮放動畫 ImageView
前陣子手機裡裝了些新 App,發現倣 Win8 主介面的設計越來越多。在大家看膩了類 GridView 與類 Tab 的排列之後,這種馬磚拼貼的風格確實讓人耳目一新。今天