高德地图SDK Android版开发 7 覆盖物示例1

前言

文本介绍Marker的常用属性、交互和碰撞示例。

示例功能如下:

  • 可设置Marker点击、拖拽、透明、旋转、可见、平贴属性状态;
  • 在地图上创建多个满足上述属性状态的Marker;
  • Marker点击事件和拖拽事件处理。

界面布局

在这里插入图片描述

  • 布局文件
<?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.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        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/map">

        <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="点击"
                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="旋转"
                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" />

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

MapMarker类

以下是MapMarker部分代码。

常量

public static final String CLICKABLE = "Clickable";
public static final String DRAGGABLE = "Draggable";
public static final String ALPHA = "Alpha";
public static final String ROTATE = "Rotate";
public static final String VISIBLE = "Visible";
public static final String FLAT = "Flat";

成员变量

// 覆盖物列表
List<BaseOverlay> overlays = new ArrayList<>();
// 选中的状态
List<String> selectedFlags = new ArrayList<>();
// 坐标点集
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点击事件

// 设置marker点击事件监听接口。
map.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        // 当一个marker对象被点击时回调此方法。
        showToast("clickable marker");
        return true;
    }
});

Marker拖拽事件

// marker拖动事件监听接口
map.setOnMarkerDragListener(new AMap.OnMarkerDragListener() {
    @Override
    public void onMarkerDragStart(Marker marker) {
        // 当marker开始被拖动时回调此方法。
    }

    @Override
    public void onMarkerDrag(Marker marker) {
        // 在marker拖动过程当中回调此方法。
    }

    @Override
    public void onMarkerDragEnd(Marker marker) {
        // 在marker拖动过程当中回调此方法。
        // 拖拽完成后更新位置
        int index = overlays.indexOf(marker);
        if (index != -1) {
            LatLng latLng = marker.getPosition();
            points.set(index, new LatLng(latLng.latitude, latLng.longitude));
        }
    }
});

创建覆盖物

public void addMarkers() {
    int[] icons = BubbleIcons.Alphabet;
    for (int i = 0; i < points.size(); ++i) {
        // 构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(icons[i]);
        // 构建MarkerOption,用于在地图上添加Marker
        MarkerOptions option = new MarkerOptions()
                .position(points.get(i)) //必传参数
                .icon(bitmap); // 必传参数
        setOption(option, i, selectedFlags);

        // 在地图上添加Marker,并显示
        Marker marker = map.addMarker(option);
        if (selectedFlags.contains(CLICKABLE))
            marker.setClickable(true); // 设置Marker覆盖物是否可以点击
        else
            marker.setClickable(false);

        overlays.add(marker);
    }
}

private void setOption(MarkerOptions option, int i, List<String> flags) {
    if (flags.contains(DRAGGABLE))
        option.draggable(true); // 设置Marker覆盖物是否可拖拽。

    if (flags.contains(ALPHA))
        option.alpha(0.5f + 0.1f * i); // 设置Marker覆盖物的透明度

    if (flags.contains(ROTATE))
        option.rotateAngle(30 * i); // 设置Marker覆盖物的图片旋转角度,从正北开始,逆时针计算。

    if (flags.contains(VISIBLE))
        option.visible(true); // 设置Marker覆盖物是否可见。
    else
        option.visible(false);

    if (flags.contains(FLAT))
        option.setFlat(true); // 设置Marker覆盖物是否平贴地图。
}

移除覆盖物

public void removeOverlay() {
    // 从地图上删除所有的覆盖物(marker,circle,polyline 等对象),
    // 但myLocationOverlay(内置定位覆盖物)除外。
    boolean isKeepMyLocationOverlay = true;
    map.clear(isKeepMyLocationOverlay);

    overlays.clear();
}

设置属性

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部分代码。

控件响应事件

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
        return;

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

运行效果图

在这里插入图片描述

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值