1.首先创建一个LocationClient()的实例调用getApplicationContext()方法获取全局的Context参数,然后调用LocationClient的registerLocationListener()方法注册一个监听器,我们注册的监听器使它实现BDLocationLisrener接口,并且通过BDLocation的方法获取相应的功能,记得加线程。
runOnUiThread(new Runnable() { @Override public void run() { StringBuilder currentPosition = new StringBuilder(); currentPosition.append("纬度:").append(Location.getLatitude()).append(" "); currentPosition.append("经度:").append(Location.getLongitude()).append(" "); currentPosition.append("国:").append(Location.getCountry()).append(" "); currentPosition.append("省:").append(Location.getProvince()).append(" "); currentPosition.append("市:").append(Location.getCountry()).append(" "); currentPosition.append("区:").append(Location.getDistrict()).append(" "); currentPosition.append("街道:").append(Location.getStreet()).append(" "); currentPosition.append("定位方式: "); if (Location.getLocType() == BDLocation.TypeGpsLocation) { currentPosition.append("GPS"); } else if (Location.getLocType() == BDLocation.TypeNetWorkLocation) { currentPosition.append("网络"); } positionText.setText(currentPosition);//将获取的内容显示在textView控件上 } });
private void requestLocation() { mlocationClient.start();//开启实例 }
我们可以创建一个方法实现更新的功能,在这里面创建LocationClientOption对象
private void initLocation(){ LocationClientOption option =new LocationClientOption(); option.setScanSpan(5000);//5秒更新 mlocationClient.setLocOption(option); }
并且将这个方法添加到开启方法中
private void requestLocation() {
initLocation();
mlocationClient.start();//开启实例
}
使地图显示出来首先在清单文件中放置一个mapView控件
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" />
下面我们调用SDKlnitializer的initialize()方法进行初初始化操作,下面我们重写onResume(),onDestory,onPause()方法,保证资源及时的释放。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mlocationClient = new LocationClient(getApplicationContext()); mlocationClient.registerLocationListener(new MyLocationListener()); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.bmapView); }
protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); }
在API提供了一个BaiduMap类,调用MapView的getMap()方法获取实例,得到实例后对地图进行操作,其中MapStatusUpdate的zooTo()方法接收flost类型的参数,用来设置缩放比率,将设置好的出入到BaiduMap中的animateMapStatus()方法中
LatLng类用来存放经纬度。
private void navigateTo(BDLocation location){ if (isFirstLocate){ LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());//存放经纬度 MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(update); update = MapStatusUpdateFactory.zoomBy(19f); baiduMap.animateMapStatus(update); isFirstLocate = false; } }
其中我们设置isFirstLocate是防止多次调用animateMapStatus()。
下满定位我们所在的位置
通过SDK提供的MyLocationData.Builder类来封装当前所在的位置,其实他还有个build()方法,把封装好的设置好后调用build(),调用后它会生成MyLocationData实例,将实例传到baidumap的setMyLocationData()方法中
private void navigateTo(BDLocation location){ if (isFirstLocate){ LatLng ll = new LatLng(location.getLatitude(),location.getLongitude()); MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(update); update = MapStatusUpdateFactory.zoomBy(19f); baiduMap.animateMapStatus(update); isFirstLocate = false; } MyLocationData.Builder locationBuilder = new MyLocationData.Builder(); locationBuilder.latitude(location.getLatitude());//获取经度 locationBuilder.latitude(location.getLongitude()); MyLocationData locationData = locationBuilder.build(); baiduMap.setMyLocationData(locationData); }
最后在onDestory中关闭,baidumap的setMylocationEnable()方法
baiduMap.setMyLocationEnabled(false); 开始的时候在oncreate()方法中开启它,将false改为true就好了