本文共 10125 字,大约阅读时间需要 33 分钟。
这一节我们使用Baidu的定位服务,我们同样要向上一篇博客一样下载Baidu定位的SDK。因为在这篇博客中我们已经讲过下载步骤,这里我们不在重复讲解下载的过程,不明白的可以直接去上一篇博客中学习。
这一节的讲解是以上一节中百度地图为基础的,使用的是上一节的项目工程。建议大家先看一下。其实这里的步骤和Baidu定位中提供的文档是差不多的,此处整理的目的只为加深楼主的印象。
1. 首先将jar包拷贝到libs文件夹下。
2. 通过Android Studio “File——>Project Structure——>Dependencies”中的File Dependency将我们刚才拷贝的.jar包导入。 3. 其次在main文件夹下创建一个“jniLibs”的文件夹,将包含.so文件的文件夹复制到此处。 4. 配置AndroidManifext.xml.再次声明下这里使用的项目是基于这篇博客的,在此基础上进行定位功能呢的添加。
1.初始化LocationClient类。
public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();public void onCreate() { mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener( myListener ); //注册监听函数 //以上两句卸载setContentView(R.layout.activity_main)之前。}
2. 配置定位SDK参数。
private void initLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span=1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可选,默认false,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); }
高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果;
低功耗定位模式:这种定位模式下,不会使用GPS,只会使用网络定位(Wi-Fi和基站定位);
仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位。
3. 实现BDLocationListener接口,在舰艇中对定位进行处理。这里我们做的处理是在定位处方放置定位符号,并添加连线。
@Override public void onReceiveLocation(BDLocation location) { if (location.getLocType() == BDLocation.TypeServerError) { } else if (location.getLocType() == BDLocation.TypeNetWorkException) { } else if (location.getLocType() == BDLocation.TypeCriteriaException) { } else { mTextViewLocation.setText(location.getAddress().address); currentLongtitude = location.getLongitude(); currentLatitude = location.getLatitude(); mBaiduMap.addOverlay(new MarkerOptions().position(new LatLng(currentLatitude, currentLongtitude)) .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_marka))); LatLng p = new LatLng(currentLatitude, currentLongtitude); points.add(p); } } }
结果如下:
全部代码如下:
public class MainActivity extends Activity implements View.OnClickListener { private Button mButtonStart;//开始定位按钮 private Button mButtonOne;//获取地址一按钮 private Button mButtonTwo;//获取地址二按钮 private Button mButtonLine;//划线按钮 private TextView mTextViewLocation;//显示当前的地址 MapView mMapView = null;//地图视图 BaiduMap mBaiduMap;//地图对象 //连线点的集合 private Listpoints = new ArrayList (); //通过地址获得经纬度 GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用 public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //定位 mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); //注册监听函数 //使用BaiduMap SDK //在使用SDK各组件之前初始化context信息,传入ApplicationContext SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); //获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView);//获得地图视图的对象 mBaiduMap = mMapView.getMap();//获得地图对象 //初始化搜索模块,注册事件监听 mSearch = GeoCoder.newInstance();//获得搜索的对象 mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() { @Override public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) { if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MainActivity.this, "抱歉,未能找到结果", Toast.LENGTH_LONG) .show(); return; } mBaiduMap.addOverlay(new MarkerOptions().position(geoCodeResult.getLocation()) .icon(BitmapDescriptorFactory .fromResource(R.mipmap.icon_marka))); mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(geoCodeResult .getLocation())); LatLng p = new LatLng(geoCodeResult.getLocation().latitude, geoCodeResult.getLocation().longitude);//添加点 points.add(p); String strInfo = String.format("纬度:%f 经度:%f", geoCodeResult.getLocation().latitude, geoCodeResult.getLocation().longitude); Log.d("data", strInfo); Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG).show(); } @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) { } }); //获取布局中的各个控件对象 mButtonStart = (Button) findViewById(R.id.button_start); mButtonOne = (Button) findViewById(R.id.button_one); mButtonTwo = (Button) findViewById(R.id.button_two); mButtonLine = (Button) findViewById(R.id.button_three); mTextViewLocation = (TextView) findViewById(R.id.textview_location); //设置点击事件 mButtonStart.setOnClickListener(this); mButtonOne.setOnClickListener(this); mButtonTwo.setOnClickListener(this); mButtonLine.setOnClickListener(this); PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, "5CjXwt6IZgRSfYxwA8dtOuG5"); } /* Activity关闭时将地图关闭 */ @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } /* ActivityonResume时,将地图onResume。 */ @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } /* ActivityonPause时,将地图onPause。 */ @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } /* 初始化定位 */ private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span = 1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认false,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_start: initLocation(); mLocationClient.start();//开始定位 break; case R.id.button_one: //获得天安门的地址 mSearch.geocode(new GeoCodeOption().city("北京市").address("海淀区中关村海龙大厦")); break; case R.id.button_two: //获得中关村的地址 mSearch.geocode(new GeoCodeOption().city("北京市").address("西城区西便门")); break; case R.id.button_three: //获得当地的地址 // 添加折线 OverlayOptions ooPolyline = new PolylineOptions().width(10).color(0xAAFF0000).points(points); mBaiduMap.addOverlay(ooPolyline); break; } } //获得当前的经纬度 private double currentLongtitude; private double currentLatitude; class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location.getLocType() == BDLocation.TypeServerError) { } else if (location.getLocType() == BDLocation.TypeNetWorkException) { } else if (location.getLocType() == BDLocation.TypeCriteriaException) { } else { mTextViewLocation.setText(location.getAddress().address); currentLongtitude = location.getLongitude(); currentLatitude = location.getLatitude(); mBaiduMap.addOverlay(new MarkerOptions().position(new LatLng(currentLatitude, currentLongtitude)) .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_marka))); LatLng p = new LatLng(currentLatitude, currentLongtitude); points.add(p); } } }}