編輯:關於 Android 程式設計
本篇在百度地圖基礎應用的基礎上,進一步實現定位相關功能,包括多種地圖模式切換、定位模式切換以及覆蓋物展示等。

整體思路是:在使用 SDK 各組件前先調用 SDKInitializer.initialize() 完成初始化(該方法需在 setContentView 之前調用),然後初始化地圖視圖、定位客戶端與覆蓋物,並在各個生命週期方法中管理地圖與定位的開關。
public class MainActivity extends Activity { private MapView mMapView; private BaiduMap mBaiduMap; private Context context; // 定位相關 private LocationClient mLocationClient; private MyLocationListener mLocationListener; private boolean isFirstIn = true; private double mLatitude; private double mLongtitude; // 自定義定位圖標 private BitmapDescriptor mIconLocation; private MyOrientationListener myOrientationListener; private float mCurrentX; private LocationMode mLocationMode; // 覆蓋物相關 private BitmapDescriptor mMarker; private RelativeLayout mMarkerLy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 該方法需在 setContentView 之前實現 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); this.context = this; initView(); initLocation(); initMarker(); mBaiduMap.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() + ""); // 展示 InfoWindow TextView tv = new TextView(context); tv.setBackgroundResource(R.drawable.location_tips); tv.setPadding(30, 20, 30, 50); tv.setText(info.getName()); tv.setTextColor(Color.parseColor("#ffffff")); final LatLng latLng = marker.getPosition(); Point p = mBaiduMap.getProjection().toScreenLocation(latLng); p.y -= 47; LatLng ll = mBaiduMap.getProjection().fromScreenLocation(p); InfoWindow infoWindow = new InfoWindow(tv, ll, new OnInfoWindowClickListener() { @Override public void onInfoWindowClick() { mBaiduMap.hideInfoWindow(); } }); mBaiduMap.showInfoWindow(infoWindow); mMarkerLy.setVisibility(View.VISIBLE); return true; } }); mBaiduMap.setOnMapClickListener(new OnMapClickListener() { @Override public boolean onMapPoiClick(MapPoi arg0) { return false; } @Override public void onMapClick(LatLng arg0) { mMarkerLy.setVisibility(View.GONE); mBaiduMap.hideInfoWindow(); } }); } // 初始化定位 private void initLocation() { mLocationMode = LocationMode.NORMAL; mLocationClient = new LocationClient(this); mLocationListener = new MyLocationListener(); mLocationClient.registerLocationListener(mLocationListener); LocationClientOption option = new LocationClientOption(); option.setCoorType("bd09ll"); option.setIsNeedAddress(true); option.setOpenGps(true); option.setScanSpan(1000); mLocationClient.setLocOption(option); mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked); myOrientationListener = new MyOrientationListener(context); myOrientationListener.setOnOrientationListener(new OnOrientationListener() { @Override public void onOrientationChanged(float x) { mCurrentX = x; } }); } @Override protected void onStart() { super.onStart(); // 開啟定位 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) mLocationClient.start(); // 開啟方向傳感器 myOrientationListener.start(); } @Override protected void onStop() { super.onStop(); // 停止定位 mBaiduMap.setMyLocationEnabled(false); mLocationClient.stop(); // 停止方向傳感器 myOrientationListener.stop(); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); } // 添加覆蓋物 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); } // 定位到我的位置 private void centerToMyLocation() { LatLng latLng = new LatLng(mLatitude, mLongtitude); MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); mBaiduMap.animateMapStatus(msu); } private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { MyLocationData data = new MyLocationData.Builder() .accuracy(location.getRadius()) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .build(); mBaiduMap.setMyLocationData(data); MyLocationConfiguration config = new MyLocationConfiguration(mLocationMode, true, mIconLocation); mBaiduMap.setMyLocationConfigeration(config); mLatitude = location.getLatitude(); mLongtitude = location.getLongitude(); if (isFirstIn) { LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); mBaiduMap.animateMapStatus(msu); isFirstIn = false; Toast.makeText(context, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } } } }
關鍵要點:onStart()/onStop() 中開關定位與方向傳感器;onResume()/onPause()/onDestroy() 中同步調用 mMapView 對應的生命週期方法以管理地圖;通過 onOptionsItemSelected 切換地圖類型(普通、衛星、交通)、定位模式(普通、跟隨、羅盤)以及添加覆蓋物。
Android Studio 代碼著色主題插件推薦
前言:將近半個月沒寫部落格了,放個假回來整個人都鬆懈下來,懶得動彈,這才真正體會到「玩物喪志」的意思。好在公司項目趕得緊,不想幹活都不行,硬是把自己拉回正軌。正因為日以繼
Android 開發深入解析:Activity 的組成、生命週期與管理
【認識 Activity】 Activity 是應用程式中的一個元件,它為使用者提供一個可互動的畫面,用來完成某項具體任務,例如撥號、拍照、寄送郵件或查看地圖。每個 Ac
Android 個人理財工具(四):添加賬單頁面(下)
本文把添加賬單介面的版面整理完成。動手前以為不會太難,實際做下去才發現排列版面配置的複雜程度甚至超過撰寫程式邏輯。上網搜尋時,這類版面的可用資料很少,官方範例用得最多的仍
Android Flux 架構入門:單向資料流的實作演練
Flux 不是框架,而是一套處理 UI 狀態的思路。正因為它只是一套約定,所以在 Android 上採用時不需要引入任何新的程式庫,你只要理解並遵循這套模式。 Flux