地圖實時定位我的位置
阿新 • • 發佈:2018-11-30
首先需要顯示地圖,請看我的另一篇文章
Android 使用百度地圖API來顯示地圖
然後實時獲取定位資訊中的經度和緯度,
Android 獲取LocationProvider以及獲取定位資訊
然後啟動定位功能標記我的位置
- 開啟定點陣圖層 setMyLocationEnabled(true)
- 構造定位資料 MyLocationData物件
- 設定定位資料,並配置定點陣圖層的資訊
- 關閉定點陣圖層 setMyLocationEnabled(false)
Activity中的程式碼如下:
private MapView mMapView; public final static String TAG = "Location"; private BaiduMap mBaiduMap; //定義百度地圖物件 //記錄是否第一次定位,然後在locationUpdates()方法中設定邏輯 private boolean isFirstLoc = true; //當前定位模式 private MyLocationConfiguration.LocationMode locationMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化地圖SDK, //需要在setContentView(R.layout.activity_main);上面 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); initMap(); //初始化地圖 locationProvice();//位置服務 } private void initMap() { //獲取地圖控制元件引用 mMapView = findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); //獲取百度地圖物件 } /** * 初始化位置服務,獲取當前所在位置 */ private void locationProvice() { //獲取位置服務 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); //獲取最佳的LocationProvider //建立一個過濾條件物件 //需要加入許可權 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> Criteria criteria = new Criteria(); //設定為不收費的 criteria.setCostAllowed(false); //使用精度最準確的 criteria.setAccuracy(Criteria.ACCURACY_FINE); //設定中等耗電量 criteria.setPowerRequirement(Criteria.POWER_MEDIUM); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, //指定位置提供者 1000, //間隔時間 1, //位置間隔1米 new LocationListener() {//監聽GPS定位資訊是否改變 @Override public void onLocationChanged(Location location) { //GPS資訊發生改變時,回撥 locationUpdates(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { //GPS狀態發生改變時,回撥 } @Override public void onProviderEnabled(String provider) { //定位提供者啟動時回撥 } @Override public void onProviderDisabled(String provider) { //定位提供者關閉時回撥 } } ); //獲取最新的定位資訊 Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); //將最新的定位資訊傳遞給locationUpdates()方法 locationUpdates(location); } public void locationUpdates(Location location) { if (location != null) { LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); //建立一個字串構建器,用於記錄位置資訊 StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("您的位置是: \n"); stringBuilder.append("經度: "); stringBuilder.append(location.getLongitude()); stringBuilder.append("\n 緯度:"); stringBuilder.append(location.getLatitude()); Log.i(TAG, stringBuilder.toString()); if (isFirstLoc) { //更新座標位置 MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); //設定地圖位置 mBaiduMap.animateMapStatus(update); isFirstLoc=false; } //構造定位資料 MyLocationData locationData = new MyLocationData.Builder() .accuracy(location.getAccuracy()) //設定精度 .direction(0) //設定方向資訊 .latitude(location.getLatitude()) //設定緯度座標 .longitude(location.getLongitude()) //設定緯度座標 .build(); mBaiduMap.setMyLocationData(locationData); //設定自定義圖示 BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo); //設定定位模式 locationMode = MyLocationConfiguration.LocationMode.NORMAL; //設定構造方式 MyLocationConfiguration configuration = new MyLocationConfiguration( locationMode,true,bitmapDescriptor ); //顯示定點陣圖標 mBaiduMap.setMyLocationConfiguration(configuration); //然後再onStart()方法和onStop中新增 //開啟定點陣圖層 //mBaiduMap.setMyLocationEnabled(true); //停止定點陣圖層 // mBaiduMap.setMyLocationEnabled(false); } else { Log.i(TAG ,"沒有獲取到位置資訊"); } } @Override protected void onStart() { super.onStart(); //開啟定點陣圖層 mBaiduMap.setMyLocationEnabled(true); } @Override protected void onStop() { super.onStop(); mBaiduMap.setMyLocationEnabled(false); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); mMapView = null; }