android高德地圖網路路徑實現自定義marker並點擊彈出自定義視窗

来源:https://www.cnblogs.com/revolve/archive/2018/04/25/8931125.html
-Advertisement-
Play Games

android中使用地圖的地方隨處可見,今天記錄一下網路路徑生成自定義marker,點擊標記彈出自定義的視窗(在這裡使用的是高德地圖) 在這裡我們使用Grilde去載入網路圖片,因為這個簡直太方便了! 在android studio下,在app/build.gradle文件當中添加如下依賴: com ...


android中使用地圖的地方隨處可見,今天記錄一下網路路徑生成自定義marker,點擊標記彈出自定義的視窗(在這裡使用的是高德地圖)

在這裡我們使用Grilde去載入網路圖片,因為這個簡直太方便了!

在android studio下,在app/build.gradle文件當中添加如下依賴:

 

compile 'com.github.bumptech.glide:glide:3.7.0'

接下來就是代碼的實現部分

  

代碼註釋的比較詳細

/**
 * Created by Yyyyq on 2018/4/20 0009.
 *    根據網路路徑生成自定義marker,點擊彈出自定義窗體
 */

public class CustomMapActivity extends AppCompatActivity implements AMap.OnMarkerClickListener,
        AMap.InfoWindowAdapter,AMap.OnMapClickListener{


    //標題頭
    private TextView textView;
    //返回按鈕
    private ImageView back;

    private MapView mMapView;
    private AMap aMap;
    Marker marker;
    //指向當前的marker(用於控制infowindow顯示與消失)
    Marker nowMarker;
      
    SimpleVO tempSimpleVO;
   



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custommap);
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        
        init();
        
        back=(ImageView)findViewById(R.id.back);
        textView = (TextView) findViewById(R.id.toolbar_title);
        Toolbar toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);
        textView.setText("生成網路圖片Marker");
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        setTranslucentBar();

        //返回按鈕
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
    private void init() {
        if (aMap == null) {
            aMap = mMapView.getMap();
            setUpMap();
            //獲取後臺數據源
            getDataSource();
           
        }
    }
    
    /**
     * 設置aMap屬性
     */
    private void setUpMap() {
        aMap.setOnMarkerClickListener(this);// 設置點擊marker事件監聽器
        aMap.setInfoWindowAdapter(this);//自定義彈出窗體
        aMap.setOnMapClickListener(this);//地圖點擊事件
    }
    
    /**
     * @Description:沉浸式標題
     */
    protected void setTranslucentBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
  
  
    
    
    /**
     *在這裡模擬方法請求到數據源,可根據實際
     *    我們用自定義實體類SimpleVO接收數據源
     */
     private void getDataSource(){
            //網路請求 得到List
            List<SimpleVO> list="後臺傳遞的數據源";
            
            if(list.size()>0){
                //根據第一條數據經緯度經地圖移動到所視區域
                LatLng curLatlng = new LatLng(Double.parseDouble(list.get(0).getLatitude()),Double.parseDouble(list.get(0).getLongitude()));
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 14f));
                for(int i=0;i<list.size();i++){
                    final int j = i;
                    //利用強大的Glide載入圖片,可放占位符等,可百度Glide屬性
                    Glide.with(CustomMapActivity.this)
                        .load(list.get(i).getUrl())
                        .into(new SimpleTarget<GlideDrawable>(){
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                //展示圖片
                                View view = LayoutInflater.from(CustomMapActivity.this).inflate(
                                        R.layout.map_userimg, null);
                                RoundImageView imageView = (RoundImageView) view.findViewById(R.id.user_img);
                                tempSimpleVO=list.get(j);
                                imageView.setImageDrawable(resource);
                                Bitmap bitmap = convertViewToBitmap(view);
                                MarkerOptions markerOptions = new MarkerOptions();
                                markerOptions.anchor(0.5f, 1.0f);
                                markerOptions.position(new LatLng(Double.parseDouble(tempSimpleVO.getLatitude()),Double.parseDouble(tempSimpleVO.getLongitude())));
                                markerOptions.draggable(false);
                                markerOptions.title(tempSimpleVO.getName());
                                markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
                                marker = aMap.addMarker(markerOptions);
                                //把相應的對象賦給marker,adapter中通過這個對象給控制項賦值
                                marker.setObject(tempSimpleVO);
                            }
                        });



                }
            }

     }


    /**
     *  點擊marker彈出視窗
     *  返回true 地圖不移動中心點
     */
    @Override
    public boolean onMarkerClick(Marker marker) {
        nowMarker=marker;
        nowMarker.showInfoWindow();
        return true;
    }
    /**
     * 自定義視窗佈局
     */
    @Override
    public View getInfoWindow(Marker marker) {
        View infoContent = LayoutInflater.from(CustomMapActivity.this).inflate(
                R.layout.item_map_window, null);
        render(marker, infoContent,2);
        return infoContent; 
    }
    
    /**
     * 對視窗信息賦值
     */
    public void render(Marker marker, View view,int mark) {
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.window_linearlayout);
        //設置透明度
        layout.getBackground().setAlpha(240);
        TextView name = (TextView) view.findViewById(R.id.window_name);
        TextView info = (TextView) view.findViewById(R.id.window_info);
        SimpleVO simpleVO = (SimpleVO) marker.getObject();
        name.setText(simpleVO.getName());
        info.setText(simpleVO.getInfo());
    }
    
    /**
     * 因自定義視窗 返回null
     */
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    /**
     *  重寫地圖點擊事件,點擊地圖任意點隱藏infowindow視窗
     */
    @Override
    public void onMapClick(LatLng latLng) {
        //隱藏infowindow視窗
        nowMarker.hideInfoWindow();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }


    /**
     * view轉bitmap
     */
    private static Bitmap convertViewToBitmap(View view){
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }



}

 接下來就是主頁面的佈局  activity_custommap

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

    <!--標題欄-->

        <android.support.v7.widget.Toolbar
            android:id="@+id/activity_main_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:paddingTop="16dp"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/bluestyle_button">
            <ImageView
                android:id="@+id/back"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center|left"
                android:src="@drawable/jiantouzuo"/>

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#fff"
                android:textSize="18sp"/>
        </android.support.v7.widget.Toolbar>


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

</LinearLayout>

代碼中應用的實力類  SimpleVO

public class SimpleVO {
    
    private String id;
    
    private String name;
    
    private String latitude;
    
    private String longitude;
    
    private String url;
    
    private String info;
    
    //省略get,set,toString方法,需自己導入


}

之後需要展示我們的自定義頭像   map_userimg  因為展示的是頭像,我們用到了圓形工具類,若沒有該工具類可查看複製上一篇隨筆,可直接粘貼複製

<?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"
    android:gravity="center">


    <!--自定義圓形工具類-->
    <!--若沒有該工具類可賦值上一篇代碼,Ctrl+c Ctrl+v可用-->
    <com.bc.yyyyq.util.RoundImageView
        android:id="@+id/user_img"
        android:scaleType="fitCenter"
        android:layout_width="30dp"
        android:layout_height="30dp"/>



</LinearLayout>

最後我們需要展示我們自定義的marker的彈出窗體   item_map_window

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/window_linearlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/brokermap_layout"
    android:padding="10dp"
    android:orientation="vertical">

    <!--姓名-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="姓名:"
            android:textColor="#515151"
            android:textSize="14sp"/>
        <TextView
            android:id="@+id/window_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center|left"
            android:text=""
            android:textColor="#515151"
            android:textSize="14sp"/>
    </LinearLayout>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f2f2f2" />
    <!--個人信息-->
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="個人信息:"
            android:gravity="center"
            android:textColor="#515151"
            android:textSize="14sp"/>
        <TextView
            android:id="@+id/window_info"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|left"
            android:layout_marginLeft="5dp"
            android:text=""
            android:textColor="#515151"
            android:textSize="14sp"/>
    </LinearLayout>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f2f2f2" />
    

</LinearLayout>

就這樣就完成了自定義網路圖片的marker和點擊標識彈出我們自定義的視窗,想要的效果就展示出來了


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

-Advertisement-
Play Games
更多相關文章
  • SQL中的LEFT RIGHT INNER JOIN的區別及學習,僅供參考,如有錯誤,歡迎糾正。 ...
  • 寫在前面: 最近,老項目新增了日報優化的需求,麗姐讓我用觸發器去實現當數據插入或者更新的時候,實現對日報表數據更新操作。嗯嗯嗯呢,之前學習資料庫的時候,有碰到過觸發器,但都是一跳而過,也沒怎麼去真正的實踐,這次就權當再次去學習吧~~ 1.觸發器實例: 上面的觸發器例子,只是一個框架,並不能執行,這裡 ...
  • 最近升級android studio到版本3.0.1後,想要使用FragmentActivity這個類,導入v4包,發現R文件報錯了,也就是找不到的意思。 如圖:導包 此時選中v4包導進去。 確定之後它一下子就跑到這裡來,這是Values文件。查看了一下,還真沒有R文件的相關內容。 再看下build ...
  • 最近版本測試階段,發現一個奇怪的問題,以前在A測試機上出現的崩潰bug,解決後今天在B測試機上又出現了,在B上解決完之後,返回到設備A上發現又不行了。最後調試發現是測試設備系統版本不同導致的,A設備是iOS10的,而B設備是iOS11的,需求是,當點擊自定義的UITableViewCell時,需要它 ...
  • 最近使用cocoapods集成友盟 發現幾個經典錯誤 1.clang: error: linker command failed with exit code 1 (use -v to see invocation) 這個 應該是配置路徑 有問題。 我的解決方案是:找到other link 刪除所有 ...
  • Android studio更新 第一步:在你所在項目文件夾下:你項目根目錄gradlewrapper gradle-wrapper.properties (只要在打開項目的時候選OK,這個文件就會出現) 修改gradle-wrapper.properties最後一行的地址我的是: 1 distri ...
  • 註意事項: sql 使用單引號來環繞文本值(大部分資料庫系統也接受雙引號)。如果是數值,請不要使用引號。 一、資料庫 1、創建資料庫 創建一個名字為lesson的資料庫 2、刪除資料庫 二:表 1、創建表 CREATE TABLE .`it` ( INT NOT NULL, VARCHAR(45) ...
  • 本期知識點: 兩大常用佈局的簡單介紹 在我們的APP使用第三方庫 Android Studio常用快捷鍵 兩大常用佈局的簡單介紹 在我們的APP使用第三方庫 Android Studio常用快捷鍵 一、兩大常用佈局 1.LinearLayout線性佈局 線性佈局,可以垂直顯示或者水平顯示,設置Lin ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...