(Android)调用百度地图api之添加覆盖物
16lz
2021-01-23
感谢 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 Listinfos = 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(Listinfos){ 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" />
更多相关文章
- Android在程序中动态生成控件,动态布局
- android 相对布局例子代码
- Android Studio 添加百分比布局库的依赖报错 第一行代码
- 简单布局收缩动画
- Android 下实现带有图标和Checkbox的 ListView Item(2)
- Android 基础布局控件自定义view使用练习