欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android高德自定义marker点标记与infoWindow窗体

程序员文章站 2022-04-03 21:29:34
自定义marker与infoWindow窗体 1.展示地图布局的xml

Android高德自定义marker点标记与infoWindow窗体自定义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>