百度地图开发地图覆盖物的Demo

用android studio来实现在百度地图上添加覆盖物和显示覆盖物的相关信息(如经纬度,地点名称,相关的描述)
1.构建一个实体类来存放覆盖物的相关信息

package com.example.global_1;

import java.io.Serializable;

public class MarkerInfoUtil implements Serializable{
    private static final long serialVersionUID = 8633299996744734593L;

    private double latitude;//纬度
    private double longitude;//经度
    private String name;//名字
    private String description;//描述
    //构造方法
    public MarkerInfoUtil() {}
    public MarkerInfoUtil(double latitude, double longitude, String name,  String description) {
        super();
        this.latitude = latitude;
        this.longitude = longitude;
        this.name = name;
        this.description = description;
    }
    //toString方法
    @Override
    public String toString() {
        return "MarkerInfoUtil [latitude=" + latitude + ", longitude=" + longitude + ", name=" + name + ", description=" + description + "]";
    }
    //getter setter
    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

2.在activity_main.xml布局文件中设计相应的面板

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

   <RelativeLayout
        android:id="@+id/rl_marker"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:background="#ffffff"
        android:visibility="gone"
        android:clickable="true"><!-- 如果不添加这个属性,当点击布局时,会和地图点击事件干扰 -->
        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_margin="10dp"/>

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/iv_img"
            android:layout_marginBottom="5dp"
            android:textColor="#000000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_name"
            android:textSize="14sp"
            android:textColor="#000000"/>
    </RelativeLayout>
    <com.baidu.mapapi.map.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:clickable="true" />


        <Button
            android:id="@+id/ib_marker"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="12"
            android:padding="10dip"
            android:text="地图覆盖物" />



</RelativeLayout>

3.实例化实体类中的覆盖物信息

private void setMarkerInfo() {
        infos = new ArrayList<MarkerInfoUtil>();
        infos.add(new MarkerInfoUtil(116.41142,39.90811,"王府井","王府井是北京最有名的商业街"));
        infos.add(new MarkerInfoUtil(116.418076,39.908283,"东单","东单因一座牌坊而命名"));

    }

4.显示marker

private void addOverlay(List<MarkerInfoUtil> infos) {
        //清空地图
        mBaiduMap.clear();
        //创建marker的显示图
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_foreground);
        LatLng latLng = null;
        Marker marker;
        OverlayOptions options;
        for(MarkerInfoUtil info:infos){
            //获取经纬度
            latLng = new LatLng(info.getLatitude(),info.getLongitude());
            //设置marker
            options = new MarkerOptions()
                    .position(latLng)//设置位置
                    //.icon(bitmap)//设置图标样式
                    .zIndex(9) // 设置marker所在层级
                    .draggable(true); // 设置手势拖拽;
            //添加marker
            marker = (Marker) mBaiduMap.addOverlay(options);
            //使用marker携带info信息,当点击事件的时候可以通过marker获得info信息
            Bundle bundle = new Bundle();
            //info必须实现序列化接口
            bundle.putSerializable("info", info);
            marker.setExtraInfo(bundle);
        }
        //将地图显示在最后一个marker的位置
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
        mBaiduMap.setMapStatus(msu);
        //添加marker点击事件的监听

    }

5.实例化按钮控件并为它设置一个监听器实现覆盖物的开启和关闭

private void initView() {
        ib_marker = (Button)findViewById(R.id.ib_marker);
        ib_marker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(v.getId()==R.id.ib_marker) {
                            //显示marker
                            showInfo("显示覆盖物");
                            addOverlay(infos);
                            showMarker = true;
               }else{
                            //关闭显示marker
                            showInfo("关闭覆盖物");
                            mBaiduMap.clear();
                            showMarker = false;
                        }

            }
        });

    }

百度地图开放平台链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值