自訂 MapView 類別

      在〈自訂 MapView 類別〉中尚無留言

自訂 MapView 類別

繼上一篇 Display user’s location 的說明,將MapView 元件置於 layout 內雖說方便,但還需撰寫很多的代碼來控制MapView。若把這些控制代碼寫在 MainActivity,會讓 MainActivity 變的肥大而不好維護。

所以最好的方法就是自訂 MapView 類別,然後把控制代碼寫在自訂類別之內。

activity_main Layout

Layout 中新增一個名為 map 的 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:orientation="horizontal" />
</androidx.constraintlayout.widget.ConstraintLayout>

todo

自訂類別

本例將自訂類別命名為MahalMap,需繼承MapView父類別,然後將所有的控制代碼寫在此類別中。

🔒 更多內容,請登入會員繼續閱讀。

立即登入

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *