Android高德自定義marker點標記與infoWindow窗體

来源:https://www.cnblogs.com/YyyyQ/archive/2020/03/24/12559126.html
-Advertisement-
Play Games

自定義marker與infoWindow窗體 1.展示地圖佈局的xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" an ...


自定義marker與infoWindow窗體

 

 

1.展示地圖佈局的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2.Activity自定義marker與infowindow(代碼註釋詳細)

 

/**
 * Created by YyyyQ on 2020/3/24
 */
public class MainActivity extends AppCompatActivity implements AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnMapClickListener {

    private MapView mapView = null;
    private AMap aMap;
    private UiSettings uiSettings;
    //存放所有點的經緯度
    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
    //當前點擊的marker
    private Marker clickMaker;
    //自定義窗體
    View infoWindow = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
            uiSettings = aMap.getUiSettings();
            //設置地圖屬性
            setMapAttribute();
        }

        //模擬數據源
        List<Map<String, String>> list = getData();

        //循壞在地圖上添加自定義marker
        for (int i = 0; i < list.size(); i++) {
            LatLng latLng = new LatLng(Double.parseDouble(list.get(i).get("latitude")), Double.parseDouble(list.get(i).get("longitude")));
            MarkerOptions markerOption = new MarkerOptions();
            markerOption.position(latLng);
            markerOption.title(list.get(i).get("title"));
            markerOption.snippet(list.get(i).get("content"));
            //自定義點標記的icon圖標為drawable文件下圖片
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ditu_yiliao)));
            markerOption.draggable(false);
            aMap.addMarker(markerOption);
            //將所有marker經緯度include到boundsBuilder中
            boundsBuilder.include(latLng);
        }
        //更新地圖狀態
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 10));
    }


    /**
     * 設置地圖屬性
     */
    private void setMapAttribute() {
        //設置預設縮放級別
        aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
        //隱藏的右下角縮放按鈕
        uiSettings.setZoomControlsEnabled(false);
        //設置marker點擊事件監聽
        aMap.setOnMarkerClickListener(this);
        //設置自定義信息視窗
        aMap.setInfoWindowAdapter(this);
        //設置地圖點擊事件監聽
        aMap.setOnMapClickListener(this);
    }

    /**
     * 模擬數據源
     */
    private List<Map<String, String>> getData() {
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("title", "標題NO." + i);
            map.put("content", "這是第" + i + "個marker的內容");
            map.put("latitude", (43 + Math.random() + "").substring(0, 9));
            map.put("longitude", (125 + Math.random() + "").substring(0, 10));
            list.add(map);
        }
        return list;
    }

    /**
     * marker點擊事件
     */
    @Override
    public boolean onMarkerClick(Marker marker) {
        clickMaker = marker;
        //點擊當前marker展示自定義窗體
        marker.showInfoWindow();
        //返回true 表示介面已響應事,無需繼續傳遞
        return true;
    }

    /**
     * 監聽自定義視窗infoWindow事件回調
     */
    @Override
    public View getInfoWindow(Marker marker) {
        if (infoWindow == null) {
            infoWindow = LayoutInflater.from(this).inflate(R.layout.amap_info_window, null);
        }
        render(marker, infoWindow);
        return infoWindow;
    }

    /**
     * 自定義infoWindow視窗
     */
    private void render(Marker marker, View infoWindow) {
        TextView title = infoWindow.findViewById(R.id.info_window_title);
        TextView content = infoWindow.findViewById(R.id.info_window_content);
        title.setText(marker.getTitle());
        content.setText(marker.getSnippet());
    }

    /**
     * 不能修改整個InfoWindow的背景和邊框,返回null
     */
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    /**
     * 地圖點擊事件
     * 點擊地圖區域讓當前展示的窗體隱藏
     */
    @Override
    public void onMapClick(LatLng latLng) {
        //判斷當前marker信息視窗是否顯示
        if (clickMaker != null && clickMaker.isInfoWindowShown()) {
            clickMaker.hideInfoWindow();
        }

    }
}

 

3.自定義infoWindow窗體佈局

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/infowindow"
    android:orientation="vertical">
    <!--@drawable/infowindow為.9圖-->

    <TextView
        android:id="@+id/info_window_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="#333"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/info_window_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="#333"
        android:textSize="12sp" />

</LinearLayout>

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • MySQL整理 這幾天整理了MySQL的知識點,還有一部分需要在梳理一下,圖上先寫這麼多吧。 未完待續…… ...
  • 從監控工具DPA中發現一個資料庫(SQL Server 2008 R2)的等待事件突然彪增,下鑽分析發現資料庫執行存儲過程sp_MailItemResultSets時,引起了非常嚴重的等待(High Wait),而主要的等待事件為PREEMPTIVE_OS_WAITFORSINGLEOBJEC。 如... ...
  • 2.3 NiFi Processor應用介紹對於NiFi的使用者來說,如果想要創建一個高效的數據流,那麼就需要瞭解什麼樣的單元處理器才最適合這個數據流。NiFi擁有大量的可以用於各種業務場景的單元處理器可供使用者挑選和使用,這些單元處理器主要提供例如系統之間數據的傳輸,數據的路由,數據的轉換、處理、... ...
  • 面試階段大家基本都會問一些mysql的題,具體的高深理論以後再慢慢補充,但是刷題是不可避免的,下麵直接上貨 創建/刪除表和索引系列 創建表 sql CREATE TABLE if not exists ( int(11) NOT NULL AUTO_INCREMENT, date DEFAULT N ...
  • 首先,SQL語句應該考慮哪些安全性? 第一,防止SQL註入,對特殊字元進行過濾、轉義或者使用預編譯的SQL語句綁定變數。 第二,當SQL語句運行出錯時,不要把資料庫返回的錯誤信息全部顯示給用戶,以防止泄露伺服器和資料庫相關信息。 其次,什麼叫做SQL註入呢,如何防止呢? 舉個例子: 你後臺寫的Jav ...
  • 今天上課要用管理員系統驗證登錄 Oracle,提示我許可權不足,上網搜了下,問題應該是當前用戶未在 ora_dba 組下,得勒,把它添加進去不就行了 找了半天,坑爹地發現 win10 家庭版它就沒有 本地用戶和組 的圖形界面,這氣人玩意兒,再去找管理本地用戶和組的 Dos 命令,把本此添加過程記錄下 ...
  • 目錄 "臟讀(Dirty reads)" "不可重覆讀(Non repeatable reads)" "幻影讀(Phantom reads)" "可重覆讀級別下防止幻讀" "可串列化級別杜絕幻讀" "總結" MySQL8中隔離級別的變數跟之前的版本不一樣,之前是tx_isolation,MySQL8 ...
  • 先上效果圖,預設的實在是太醜了,搜查 Share Extension 自定義界面相關文章大部分都會引導你用 NSExtensionPrincipalClass ,然後繼承 UIViewController ,然後全都是手寫代碼,但是我想用 Storyboard 啊!其實很簡單: 不用改 NSExte ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...