Android地圖新增標記和文字【程式碼片段】
阿新 • • 發佈:2018-11-24
Android百度地圖SDK版本:5.2
1.建立並在專案中配置好AK;
2.Application中初始化地圖元件:SDKInitializer.initialize(mContext);
3.自己頁面中使用如下:
<span style="font-family:Microsoft YaHei;">/** * 考勤詳細頁 * Created by WnsnailYang on 2014/7/7. */ @EActivity(R.layout.activity_attendance_detail) public class AttendanceDetailActivity extends BaseActivity { @Extra("attendanceInfoModel") AttendanceInfoModel attendanceInfoModel; @ViewById(R.id.header_layout) BaseHeadBar header_layout;//頭部標題 @ViewById(R.id.map_layout) RelativeLayout mapView_layout; @ViewById(R.id.tv_no_loc) TextView tv_no_loc; @ViewById(R.id.attendance_detail_grid_photo) NoScrollGridView photoGrid;//相片縮圖GridView @ViewById(R.id.attendance_detail_tv_type) TextView tv_type; @ViewById(R.id.attendance_detail_tv_time) TextView tv_time; //定位 @ViewById(R.id.bmapView) MapView mMapView; //地圖 private BaiduMap mBaiduMap; private LatLng mLatLng; private BitmapDescriptor mBitmapDescriptor; //圖片 private ArrayList<PhotoInfoModel> photosList = new ArrayList<PhotoInfoModel>();//照片URL集合 private PhotoGridAdapter photoGridAdapter;//相片GridView介面卡 @AfterViews void init() { //在使用SDK各元件之前初始化context資訊,傳入ApplicationContext header_layout.setMidTitle(getString(R.string.attendance_detail)); //設定標題 tv_type.setText(attendanceInfoModel.placeName); tv_time.setText(attendanceInfoModel.updateTime); initPhoto(); initMap(); } private void initPhoto() { photosList = attendanceInfoModel.picList; if (BaseUtils.listIsNullOrEmpty(photosList)) { return; } //設定圖片瀏覽 photoGridAdapter = new PhotoGridAdapter(this); photoGridAdapter.setPhotosList(photosList); photoGridAdapter.setDisplayDelImage(false); photoGrid.setAdapter(photoGridAdapter); photoGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { AppUtils.imageBrower(AttendanceDetailActivity.this, position, photosList); } }); photoGridAdapter.notifyDataSetChanged(); } /** * 初始化地圖設定,新增標註資訊 */ private void initMap() { mBaiduMap = mMapView.getMap(); mLatLng = getLatLng(); if (mLatLng == null) { tv_no_loc.setVisibility(View.VISIBLE); mMapView.setVisibility(View.GONE); return; } setCenterMap();//首先設定中心點 addOverlay();//新增標註 addInfoWindow();//新增覆蓋文字 } /** * 新增覆蓋文字 */ private void addInfoWindow() { View view = getLayoutInflater().inflate(R.layout.map_popview_child, null); TextView textView = (TextView) view.findViewById(R.id.titlename); textView.setText(attendanceInfoModel.address); InfoWindow.OnInfoWindowClickListener listener = new InfoWindow.OnInfoWindowClickListener() { @Override public void onInfoWindowClick() { mBaiduMap.hideInfoWindow(); } }; // 建立InfoWindow InfoWindow mInfoWindow = new InfoWindow(view, mLatLng, 0); // 顯示InfoWindow mBaiduMap.showInfoWindow(mInfoWindow); } /** * 新增標註 */ private void addOverlay() { //這裡以後可以做成動畫效果 // ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); // giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.ico_location_black)); // giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.ico_location_arrow)); mBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ico_location_arrow); OverlayOptions options = new MarkerOptions().position(mLatLng).icon(mBitmapDescriptor).zIndex(1).draggable(true);//設定:座標-圖示-z軸索引-可拖拽 mBaiduMap.addOverlay(options); } /** * 設定中心點 */ private void setCenterMap() { //定義地圖狀態 MapStatus mMapStatus = new MapStatus.Builder().target(mLatLng).zoom(18).build(); //定義MapStatusUpdate物件,以便描述地圖狀態將要發生的變化 MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); //改變地圖狀態 mBaiduMap.setMapStatus(mMapStatusUpdate); } private LatLng getLatLng() { double logitude = 113.15; double latitude = 23.06; //定義文字所顯示的座標點 if (!TextUtils.isEmpty(attendanceInfoModel.longitude)) { logitude = Double.parseDouble(attendanceInfoModel.longitude); } else { return null; } if (!TextUtils.isEmpty(attendanceInfoModel.latitude)) { latitude = Double.parseDouble(attendanceInfoModel.latitude); } else { return null; } return new LatLng(latitude, logitude); } @Override public void onDestroy() { super.onDestroy(); //在activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命週期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity執行onResume時執行mMapView. onResume (),實現地圖生命週期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity執行onPause時執行mMapView. onPause (),實現地圖生命週期管理 mMapView.onPause(); } } </span>