用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;
}
}
});
}