最简单的获取定位,没有地图、导航。
先到百度地图官网下载需要的jar包,并导入。
/** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (!isLocationEnable(MainActivity.this)) { toast("定位没打开"); Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); MainActivity.this.startActivityForResult(locationIntent, REQUEST_CODE_LOCATION_SETTINGS); return; } if (location == null) { toast("定位失败"); } else { String x = String.valueOf(location.getLatitude()); String y = String.valueOf(location.getLongitude()); Log.d("定位:", x + ":" + y); // toast("坐标点 x:" + x + ",y" + y); } } public void onReceivePoi(BDLocation poiLocation) { } }
/** * 百度定位 */ public void initBaiDuLocation() { myListener = new MyLocationListenner(); mLocClient = new LocationClient(getApplicationContext()); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开gps option.setLocationNotify(true); // option.setCoorType("bd09ll"); // 设置坐标类型 // option.setCoorType("wgs84"); // 设置坐标类型 option.setCoorType("gcj02"); // 设置坐标类型 option.setIsNeedAddress(true);// option.setScanSpan(5000);// 定位间隔 option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 高精度 mLocClient.setLocOption(option); mLocClient.start(); }