Android開發之百度地圖定位以及簡單覆蓋物的實現
阿新 • • 發佈:2018-12-19
直接上程式碼:
先看下效果圖:
我這裡主要做了三個功能:
1.一秒鐘實時定位功能;
2.新增任意經緯度地點到地圖上;
3.判斷朝陽門是否在本人定位範圍1000米內;
百度地圖初始化方法:
DemoApplication.java
package com.qfy.qfymap; import android.app.Application; import com.baidu.mapapi.CoordType; import com.baidu.mapapi.SDKInitializer; public class DemoApplication extends Application { @Override public void onCreate() { super.onCreate(); // 在使用 SDK 各組間之前初始化 context 資訊,傳入 ApplicationContext SDKInitializer.initialize(this); //自4.3.0起,百度地圖SDK所有介面均支援百度座標和國測局座標,用此方法設定您使用的座標型別. //包括BD09LL和GCJ02兩種座標,預設是BD09LL座標。 SDKInitializer.setCoordType(CoordType.BD09LL); } }
mainactivity.java
package com.qfy.qfymap; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.utils.SpatialRelationUtil; /** * 此demo用來展示如何結合定位SDK實現定位,並使用MyLocationOverlay繪製定位位置 同時展示如何使用自定義圖示繪製並點選時彈出泡泡 */ public class MainActivity extends Activity implements SensorEventListener { // 定位相關 LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); private LocationMode mCurrentMode; BitmapDescriptor mCurrentMarker; private static final int accuracyCircleFillColor = 0xAAFFFF88; private static final int accuracyCircleStrokeColor = 0xAA00FF00; private SensorManager mSensorManager; private Double lastX = 0.0; private int mCurrentDirection = 0; private double mCurrentLat = 0.0; private double mCurrentLon = 0.0; private float mCurrentAccracy; MapView mMapView; BaiduMap mBaiduMap; // 初始化全域性 bitmap 資訊,不用時及時 recycle BitmapDescriptor bdA = BitmapDescriptorFactory .fromResource(R.drawable.icon_marka); BitmapDescriptor bdB = BitmapDescriptorFactory .fromResource(R.drawable.icon_markb); // UI相關 OnCheckedChangeListener radioButtonListener; Button requestLocButton; boolean isFirstLoc = true; // 是否首次定位 private MyLocationData locData; private float direction; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestLocButton = (Button) findViewById(R.id.button1); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//獲取感測器管理服務 mCurrentMode = LocationMode.NORMAL; requestLocButton.setText("普通"); OnClickListener btnClickListener = new OnClickListener() { public void onClick(View v) { switch (mCurrentMode) { case NORMAL: requestLocButton.setText("跟隨"); mCurrentMode = LocationMode.FOLLOWING; mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); MapStatus.Builder builder = new MapStatus.Builder(); builder.overlook(0); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); break; case COMPASS: requestLocButton.setText("普通"); mCurrentMode = LocationMode.NORMAL; mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); MapStatus.Builder builder1 = new MapStatus.Builder(); builder1.overlook(0); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder1.build())); break; case FOLLOWING: requestLocButton.setText("羅盤"); mCurrentMode = LocationMode.COMPASS; mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); break; default: break; } } }; requestLocButton.setOnClickListener(btnClickListener); RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup); radioButtonListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.defaulticon) { // 傳入null則,恢復預設圖示 mCurrentMarker = null; mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, null)); } if (checkedId == R.id.customicon) { // 修改為自定義marker mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.icon_geo); mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker, accuracyCircleFillColor, accuracyCircleStrokeColor)); } } }; group.setOnCheckedChangeListener(radioButtonListener); // 地圖初始化 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 開啟定點陣圖層 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 開啟gps option.setCoorType("bd09ll"); // 設定座標型別 option.setScanSpan(1000); mLocClient.setLocOption(option); mLocClient.start(); mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() { @Override public void onMapLoaded() { initOverlay(); } }); } /** * 初始化Overlay */ public void initOverlay() { // add marker overlay // LatLng llA = new LatLng(39.94019, 116.43876); LatLng llA = new LatLng(40.123798,116.28088); LatLng llB = new LatLng(39.940398, 116.438944); MarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA); Marker mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA)); MarkerOptions ooB = new MarkerOptions().position(llB).icon(bdB); Marker mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB)); MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(18.0f); mBaiduMap.setMapStatus(msu); } @Override public void onSensorChanged(SensorEvent sensorEvent) { double x = sensorEvent.values[SensorManager.DATA_X]; if (Math.abs(x - lastX) > 1.0) { mCurrentDirection = (int) x; locData = new MyLocationData.Builder() .accuracy(mCurrentAccracy) // 此處設定開發者獲取到的方向資訊,順時針0-360 .direction(mCurrentDirection).latitude(mCurrentLat) .longitude(mCurrentLon).build(); mBaiduMap.setMyLocationData(locData); } lastX = x; } @Override public void onAccuracyChanged(Sensor sensor, int i) { } /** * 定位SDK監聽函式 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 銷燬後不在處理新接收的位置 if (location == null || mMapView == null) { return; } mCurrentLat = location.getLatitude(); mCurrentLon = location.getLongitude(); mCurrentAccracy = location.getRadius(); Log.e("列印半徑", mCurrentAccracy + "=="); locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此處設定開發者獲取到的方向資訊,順時針0-360 .direction(mCurrentDirection).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } //定位成功後取到經緯度資訊 MyLocationData locationData = mBaiduMap.getLocationData(); double lt = locationData.latitude; double li = locationData.longitude; Log.e("列印經緯度1", li + "," + lt); Log.e("列印經緯度2", mCurrentLon + "," + mCurrentLat); // Toast.makeText(MainActivity.this, "朝陽門是否在融寶打卡範圍內?" + SpatialRelationUtil.isCircleContainsPoint(new LatLng(116.440149,39.939851), 30, new LatLng(116.440108, 39.93058)), Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "朝陽門是否在公司打卡範圍內?" + SpatialRelationUtil.isCircleContainsPoint(new LatLng(lt, li), 1000, new LatLng(39.93058, 116.440108)), Toast.LENGTH_LONG).show(); } public void onReceivePoi(BDLocation poiLocation) { } } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); //為系統的方向感測器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI); } @Override protected void onStop() { //取消註冊感測器監聽 mSensorManager.unregisterListener(this); super.onStop(); } @Override protected void onDestroy() { // 退出時銷燬定位 mLocClient.stop(); // 關閉定點陣圖層 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; super.onDestroy(); } }
百度地圖相關jar包庫檔案請下載原始碼檢視:
如果看不懂可以下載原始碼: