View Annotation

      在〈View Annotation〉中尚無留言

View Annotation

上一篇的Annotation,就只是一個圖示。如果希望能把 Annotation 當成一個 Button , 可以在圖示上 click 一下,然後顯示相關的資訊,此時就要改用 View Annotation。

說白話一點,”View Annotation” 就是一個可以接收事件的 Android 元件,可以是 View,Button, ImageButton 等等元件。那麼,還需要使用上一篇的 Annotation 嗎? 如果不需跟使用者互動的話,Annotation 的效能會比 View Annotation 好很多。

請注意一下官網上有說明,當View Annotation 的 Overlap (重疊) 屬性設定為 true ,而且數量大於250個時,其效能會變的很差。

annotation.xml

首先新增一個要顯示在MapBox上的Layout,命名為 annotation.xml,裏面的內容就只有一個ImageBtton

<?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">
    <ImageButton
        android:tag=""
        android:id="@+id/btn"
        android:background="#00000000"
        android:src="@drawable/red_marker"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_width="30dp"
        android:layout_height="48dp"/>
</LinearLayout>

todo

ViewAnnotationManager

viewAnnotationManager是新增 View Annotation的管理器。

MapBox在這方面的設計很奇怪,如果要新增 Annotation的話,需使用  mapview.annotations.createPointAnnotationManager 來產生 annotationManager 管理器。但  viewAnnotationManager 這個管理器卻早就存於 mapview 裏了,不需要建立就可以使用。

產生ViewAnnotation物件

使用 viewAnnotationManager.addViewAnnotation()即可產生一個 viewAnnotation物件,此物件為 View 的型態,此時這個物件尚末置入 MapBox 之中。

viewAnnotation.apply{} 可以設置View中元件的內容,如text,也可以設置事件,如 setOnClickListener。

最後,使用viewAnnotationManager.updateViewAnnotation() 將 viewAnnotation物件放到MapBox中。

🔒 底下內容僅限會員閱讀。

立即登入

發佈留言

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