感谢 hyman 的视频教学课程:https://www.imooc.com/video/5686

 

一、自定义类Info

public class Info implements Serializable{   private double latitude;   private double longitude;   private int imgId;   private String name;   public static List infos = new ArrayList();      //添加两个静态地图覆盖物   static   {      infos.add(new Info(32.120072, 118.922755, R.drawable.bookbox, "box1202"));      infos.add(new Info(32.120267, 118.920518, R.drawable.bookbox, "box1201"));   }   public Info(double latitude, double longitude, int imgId, String name)   {      this.latitude = latitude;      this.longitude = longitude;      this.imgId = imgId; //点击覆盖物,显示图片      this.name = name; //点击覆盖物,显示文字   }   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;   }}

二、核心代码 

  • 声明所需变量
// 覆盖物相关private BitmapDescriptor mMarker;public RelativeLayout mMarkerLy;
  • 核心代码 
private void initMarker() //初始化{    mMarker = BitmapDescriptorFactory.fromResource(R.drawable.bookbox); //设置地图覆盖物图标    mMarkerLy = findViewById(R.id.id_maker_ly); //布局文件中所对应的RelativeLayout}
 // 添加覆盖物private void addOverlays(List infos){    mBaiduMap.clear();    LatLng latLng = null;    Marker marker = null;    OverlayOptions options;    for (Info info : infos)    {        // 经纬度        latLng = new LatLng(info.getLatitude(), info.getLongitude());        // 图标        options = new MarkerOptions().position(latLng).icon(mMarker)                .zIndex(5);        marker = (Marker) mBaiduMap.addOverlay(options);        Bundle arg0 = new Bundle();        arg0.putSerializable("info", info);        marker.setExtraInfo(arg0);    }    MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);    mBaiduMap.setMapStatus(msu);}
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() //设置监听{    @Override    public boolean onMarkerClick(Marker marker)    {        Bundle extraInfo = marker.getExtraInfo();        Info info = (Info) extraInfo.getSerializable("info");        TextView name = mMarkerLy.findViewById(R.id.id_info_name);        boxname=info.getName(); //从info类中获取数据        /*connect();*/        name.setText(info.getName());        mMarkerLy.setVisibility(View.VISIBLE); //布局文件中的RelativeLayout设置为可见        return true;    }});mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener(){    @Override    public boolean onMapPoiClick(MapPoi arg0)    {        return false;    }    @Override    public void onMapClick(LatLng arg0)    {        mMarkerLy.setVisibility(View.GONE);        mBaiduMap.hideInfoWindow();        /*mConnect.disconnect();*/        /*adapter=null;*/    }}); 

 三、其它

android:id="@+id/id_maker_ly"    android:layout_width="match_parent"    android:layout_height="240dp"    android:layout_alignParentBottom="true"    android:background="#cc4e5a6b"    android:clickable="true"    android:visibility="gone">        android:id="@+id/id_info_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="书箱1201"        android:textColor="#fff5eb" />            

更多相关文章

  1. Android快速开发之封装标题栏
  2. Android——Activity生命周期
  3. 【Android】获取apk的版本及包名等信息
  4. Android手机开发:Menu功能
  5. Android(安卓)launcher3 -- launcher3源码1
  6. 2011.09.13(3)——— android 添加快捷方式并且图标上添加数字
  7. Android获取用户已安装app列表
  8. Android笔记【外观部分】
  9. [android]为程序创建快捷方式

随机推荐

  1. Android关机流程源码分析
  2. Android(安卓)监听锁屏/开屏事件
  3. Android 定位地址,获取经纬度,并转换为中文
  4. Android(安卓)1.5: 飞行模式分析
  5. Android中实现下拉刷新
  6. Fragment 和 Activity 全通讯
  7. android 白天模式/夜间模式切换
  8. Android轮播图 banner,图片视频混播banner
  9. Cocos2d-x在win32,android和IOS下的文件读
  10. Android的FastBoot的安装与环境变量设置