百度地圖SDK Android版開發(fā) 7 覆蓋物示例1

前言

文本介紹Marker的常用屬性、交互和碰撞示例召嘶。

示例功能如下:

  • 可設(shè)置Marker點(diǎn)擊苍蔬、拖拽、透明茎匠、旋轉(zhuǎn)、可見汽馋、平貼、碰撞和POI碰撞屬性狀態(tài)悄雅;
  • 在地圖上創(chuàng)建多個(gè)滿足上述屬性狀態(tài)的Marker宽闲;
  • Marker點(diǎn)擊事件和拖拽事件處理容诬。

界面布局

1布局.png
  • 布局文件
<?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=".MapMarkerActivity">

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:clickable="true"
        app:layout_constraintBottom_toTopOf="@id/bottomView"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bmapView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            android:paddingHorizontal="10dp">

            <CheckBox
                android:id="@+id/clickable"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:onClick="setMarkerFlag"
                android:text="點(diǎn)擊"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/draggable"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="拖拽"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/alpha"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="透明"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/rotate"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="旋轉(zhuǎn)"
                android:textColor="@color/white"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:background="@android:color/background_dark"
            android:orientation="horizontal"
            android:paddingHorizontal="10dp">

            <CheckBox
                android:id="@+id/visible"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:onClick="setMarkerFlag"
                android:text="可見"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/flat"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="平貼"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/joinCollision"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="碰撞"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/poiCollided"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="setMarkerFlag"
                android:text="POI碰撞"
                android:textColor="@color/white"
                android:textStyle="bold" />

        </LinearLayout>
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>

MapMarker類

以下是MapMarker部分代碼友雳。

常量

static final String CLICKABLE = "Clickable";
static final String DRAGGABLE = "Draggable";
static final String ALPHA = "Alpha";
static final String ROTATE = "Rotate";
static final String VISIBLE = "Visible";
static final String FLAT = "Flat";
static final String JOIN_COLLISION = "JoinCollision";
static final String POI_COLLIDED = "PoiCollided";

成員變量

// 覆蓋物列表
List<Overlay> overlays = new ArrayList<>();
// 選中的狀態(tài)
List<String> selectedFlags = new ArrayList<>();
// 坐標(biāo)點(diǎn)集
List<LatLng> points = new ArrayList<>();

初始值

selectedFlags.add(CLICKABLE);
selectedFlags.add(VISIBLE);

points.add(new LatLng(39.97923, 116.357428));
points.add(new LatLng(39.94923, 116.397428));
points.add(new LatLng(39.97923, 116.437428));
points.add(new LatLng(39.92353, 116.490705));

Marker點(diǎn)擊事件

// 設(shè)置地圖 Marker 覆蓋物點(diǎn)擊事件監(jiān)聽者,
// 自3.4.0版本起可設(shè)置多個(gè)監(jiān)聽對(duì)象,
// 停止監(jiān)聽時(shí)調(diào)用removeMarkerClickListener移除監(jiān)聽對(duì)象
map.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
    //marker被點(diǎn)擊時(shí)回調(diào)的方法
    //若響應(yīng)點(diǎn)擊事件流礁,返回true神帅,否則返回false
    //默認(rèn)返回false
    @Override
    public boolean onMarkerClick(Marker marker) {
        showToast("clickable marker");
        return true;
    }
});

Marker拖拽事件

// 設(shè)置 Marker 拖拽事件監(jiān)聽者
map.setOnMarkerDragListener(new BaiduMap.OnMarkerDragListener() {
    // 在Marker開始被拖拽時(shí)回調(diào)此方法绍填,
    // 這個(gè)Marker的位置可以通過getPosition()方法獲取 marker 被拖拽的Marker對(duì)象
    @Override
    public void onMarkerDragStart(Marker marker) {
    }

    // 在Marker拖拽過程中回調(diào)此方法,
    // 這個(gè)Marker的位置可以通過getPosition()方法獲取 marker 被拖動(dòng)的Marker對(duì)象
    @Override
    public void onMarkerDrag(Marker marker) {
        // 對(duì)marker處理拖拽邏輯
    }

    // 在Marker拖動(dòng)完成后回調(diào)此方法,
    // 這個(gè)Marker的位置可以通過getPosition()方法獲取 marker 被拖拽的Marker對(duì)象
    @Override
    public void onMarkerDragEnd(Marker marker) {
        // 拖拽完成后更新位置
        int index = overlays.indexOf(marker);
        if (index != -1) {
            LatLng latLng = marker.getPosition();
            points.set(index, new LatLng(latLng.latitude, latLng.longitude));
        }
    }
});

創(chuàng)建覆蓋物

public void addMarkers() {
    int[] icons = BubbleIcons.Alphabet;
    for (int i = 0; i < points.size(); ++i) {
        // 構(gòu)建Marker圖標(biāo)
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(icons[i]);
        // 構(gòu)建MarkerOption揭糕,用于在地圖上添加Marker
        MarkerOptions option = new MarkerOptions()
                .position(points.get(i)) //必傳參數(shù)
                .icon(bitmap); // 必傳參數(shù)
        setOption(option, i, selectedFlags);
        // 在地圖上添加Marker著角,并顯示
        Marker marker = (Marker) map.addOverlay(option);
        overlays.add(marker);
    }
}

private void setOption(MarkerOptions option, int i, List<String> flags) {
    if (flags.contains(CLICKABLE))
        option.clickable(true);
    else
        option.clickable(false);

    if (flags.contains(DRAGGABLE))
        option.draggable(true); // 設(shè)置Marker覆蓋物是否可拖拽雇寇。

    if (flags.contains(ALPHA))
        option.alpha(0.5f + 0.1f * i); // 設(shè)置Marker覆蓋物的透明度锨侯。

    if (flags.contains(ROTATE))
        option.rotate(30 * i); // 設(shè)置Marker覆蓋物的圖片旋轉(zhuǎn)角度叁怪,從正北開始奕谭,逆時(shí)針計(jì)算。

    if (flags.contains(VISIBLE))
        option.visible(true); // 設(shè)置Marker覆蓋物是否可見难捌。
    else
        option.visible(false);

    if (flags.contains(FLAT))
        option.flat(true); // 設(shè)置平貼地圖根吁,在地圖中雙指下拉查看效果合蔽。

    if (flags.contains(JOIN_COLLISION)) {
        option.isJoinCollision(true) // 設(shè)置marker參與碰撞
                .isForceDisPlay(i % 2 == 0) // 設(shè)置壓蓋時(shí) marker強(qiáng)制展示
                .priority(9);  // 設(shè)置碰撞優(yōu)先級(jí)為9
    }

    if (flags.contains(POI_COLLIDED)) {
        option.poiCollided(true); // 設(shè)置是否碰撞底圖POI
    }
}

移除覆蓋物

public void removeOverlay() {
    //map.removeOverLays(overlays);
    for (Overlay overlay : overlays)
        overlay.remove();
    overlays.clear();
}

設(shè)置屬性

public void addFlag(String flag) {
    if (!selectedFlags.contains(flag))
        selectedFlags.add(flag);

    removeOverlay();
    addMarkers();
}

public void removeFlag(String flag) {
    selectedFlags.remove(flag);

    removeOverlay();
    addMarkers();
}

MapMarkerActivity類

以下是MapMarkerActivity部分代碼沃斤。

控件響應(yīng)事件

public void setMarkerFlag(View view) {
    boolean checked = ((CheckBox) view).isChecked();
    int id = view.getId();
    String flag;
    if (id == R.id.clickable)
        flag = MapMarker.CLICKABLE;
    else if (id == R.id.draggable)
        flag = MapMarker.DRAGGABLE;
    else if (id == R.id.alpha)
        flag = MapMarker.ALPHA;
    else if (id == R.id.rotate)
        flag = MapMarker.ROTATE;
    else if (id == R.id.visible)
        flag = MapMarker.VISIBLE;
    else if (id == R.id.flat)
        flag = MapMarker.FLAT;
    else if (id == R.id.joinCollision)
        flag = MapMarker.JOIN_COLLISION;
    else if (id == R.id.poiCollided)
        flag = MapMarker.POI_COLLIDED;
    else
        return;

    if (checked)
        mapMarker.addFlag(flag);
    else
        mapMarker.removeFlag(flag);
}

運(yùn)行效果圖

2效果圖.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末组去,一起剝皮案震驚了整個(gè)濱河市从隆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖筛武,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件徘六,死亡現(xiàn)場離奇詭異待锈,居然都是意外死亡嘴高,警方通過查閱死者的電腦和手機(jī)拴驮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抹竹,“玉大人窃判,你說我怎么就攤上這事喇闸∪颊В” “怎么了刻蟹?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵片效,是天一觀的道長淀衣。 經(jīng)常有香客問我膨桥,道長只嚣,這世上最難降的妖魔是什么介牙? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮线得,結(jié)果婚禮上贯钩,老公的妹妹穿的比我還像新娘角雷。我一直安慰自己勺三,他們只是感情好祈远,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扫沼,像睡著了一般充甚。 火紅的嫁衣襯著肌膚如雪伴找。 梳的紋絲不亂的頭發(fā)上废菱,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天衰倦,我揣著相機(jī)與錄音樊零,去河邊找鬼驻襟。 笑死,一個(gè)胖子當(dāng)著我的面吹牛减牺,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼栋艳,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼嘱巾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起问拘,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤骤坐,失蹤者是張志新(化名)和其女友劉穎蕾久,沒想到半個(gè)月后僧著,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盹愚,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年愈腾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寸爆。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡仅醇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出节预,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布会傲,位于F島的核電站淌山,受9級(jí)特大地震影響泼疑,放射性物質(zhì)發(fā)生泄漏王浴。R本人自食惡果不足惜氓辣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一钞啸、第九天 我趴在偏房一處隱蔽的房頂上張望体斩。 院中可真熱鬧絮吵,春花似錦蹬敲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽伸辟。三九已至自娩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碎乃,已是汗流浹背梅誓。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國打工梗掰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留及穗,地道東北人埂陆。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像鹃栽,于是被迫代替她去往敵國和親民鼓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子摹察,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容