首先应该请求网络得到一些信息,比如要添加覆盖物的经度纬度,介绍啊之类的,然后就是怎么来添加覆盖物了。
private void addMarker(List<Info> infos) { //清掉地图上的图层 mBaidu.clear(); LatLng la = null; //声明覆盖物 Marker marker = null; //覆盖层对象 OverlayOptions options = null; for (Info info : infos) { la = new LatLng(info.getLatitude(), info.getLongitude()); //设置覆盖层的经纬,和图标,和底基层 options = new MarkerOptions().icon(mMarker).position(la).zIndex(5); //加入层 marker = (Marker) mBaidu.addOverlay(options); //传递值,为了点击事件 Bundle b = new Bundle(); b.putSerializable("info",info); marker.setExtraInfo(b); } //设置地图到最后一个位置 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(la); mBaidu.setMapStatus(msu); }
点击事件
mBaidu.setOnMarkerClickListener(new OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { Bundle extraInfo = marker.getExtraInfo(); Info info = (Info) extraInfo.getSerializable("info"); ImageView iv = (ImageView) mMarkerLy .findViewById(R.id.id_info_img); TextView distance = (TextView) mMarkerLy .findViewById(R.id.id_info_distance); TextView name = (TextView) mMarkerLy .findViewById(R.id.id_info_name); TextView zan = (TextView) mMarkerLy .findViewById(R.id.id_info_zan); iv.setImageResource(info.getImgId()); distance.setText(info.getDistance()); name.setText(info.getName()); zan.setText(info.getZan() + ""); mMarkerLy.setVisibility(View.VISIBLE); return true; } }); //地图点击事件 mBaidu.setOnMapClickListener(new OnMapClickListener() { @Override public boolean onMapPoiClick(MapPoi arg0) { // TODO Auto-generated method stub return false; } @Override public void onMapClick(LatLng arg0) { mMarkerLy.setVisibility(View.GONE); //隐藏 mBaidu.hideInfoWindow(); } });
package com.map.ditu; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Info implements Serializable { private static final long serialVersionUID = -1010711775392052966L; private double latitude; private double longitude; private int imgId; private String name; private String distance; private int zan; public static List<Info> infos = new ArrayList<Info>(); static { infos.add(new Info(34.242652, 108.971171, R.drawable.a01, "Ӣ����С�ù�", "����209��", 1456)); infos.add(new Info(34.242952, 108.972171, R.drawable.a02, "ɳ�����ϴԡ����", "����897��", 456)); infos.add(new Info(34.242852, 108.973171, R.drawable.a03, "�廷��װ��", "����249��", 1456)); infos.add(new Info(34.242152, 108.971971, R.drawable.a04, "��������С��", "����679��", 1456)); } public Info(double latitude, double longitude, int imgId, String name, String distance, int zan) { this.latitude = latitude; this.longitude = longitude; this.imgId = imgId; this.name = name; this.distance = distance; this.zan = zan; } 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 int getImgId() { return imgId; } public void setImgId(int imgId) { this.imgId = imgId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDistance() { return distance; } public void setDistance(String distance) { this.distance = distance; } public int getZan() { return zan; } public void setZan(int zan) { this.zan = zan; } }