地图实时定位我的位置怎么设置_实时定位

(30) 2024-09-28 21:01:01

首先需要显示地图,请看我的另一篇文章

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; }

 

 

 

THE END

发表回复