Android计步器悦步——百度地图

来源:互联网 发布:java获取request对象 编辑:程序博客网 时间:2024/06/05 11:35

(源代码见 https://github.com/14353350/Yuebu-Pedometer)

申请密钥

  1. 每个Key唯一对应一个APP,如果您的APP修改了包名或者发布的时候打包的签名文件改变了,则改变前后的APP被视为两个APP。因此,多个APP【包括一份代码多个包名打包】需申请多个与之对应的Key
  2. 在同一个工程中同时使用百度地图SDK、定位SDK、导航SDK 和全景SDK的全部或者任何组合,可以共用同一个key
    这里写图片描述
    SHA1简单获取方法:如图点击signingReport即可查看
    这里写图片描述

配置环境

  1. 在相关下载里下载SDK,推荐自定义下载。
  2. 在工程app/libs目录下放入baidumapapi_vX_X_X.jar包,在src/main/目录下新建jniLibs目录,将armeabi等拷入
    这里写图片描述
  3. 在AndroidManifest中添加开发密钥、所需权限等信息

    • 在application中添加开发密钥
    <application>      <meta-data          android:name="com.baidu.lbsapi.API_KEY"          android:value="开发者 key" />  </application>
    • 添加所需权限
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /><uses-permission android:name="android.permission.WAKE_LOCK"/><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_SETTINGS" />

显示地图

  1. 在布局xml文件中添加地图控件

    <com.baidu.mapapi.map.MapView      android:id="@+id/bmapView"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:clickable="true" />
  2. 在应用程序创建时初始化 SDK引用的Context 全局变量

    protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //在使用SDK各组件之前初始化context信息,传入ApplicationContext    //注意该方法要再setContentView方法之前实现    SDKInitializer.initialize(getApplicationContext());    setContentView(R.layout.activity_navigate);    ...    mMapView = (MapView) findViewById(R.id.baidumapView);    //普通地图    mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);    ...
  3. 管理地图生命周期

    @Overrideprotected void onDestroy() {    super.onDestroy();    //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理    mMapView.onDestroy();}@Overrideprotected void onResume() {    super.onResume();    //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理    mMapView.onResume();}@Overrideprotected void onPause() {    super.onPause();    //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理    mMapView.onPause();}

定位

  1. 在Application标签中声明SERVICE组件

    <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"></service>
  2. 声明使用权限

    <!-- 这个权限用于进行网络定位--><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission><!-- 这个权限用于访问GPS定位--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission><!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位--><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><!-- 获取运营商信息,用于支持提供运营商信息相关的接口--><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><!-- 用于读取手机当前的状态--><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission><!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><!-- 访问网络,网络定位需要上网--><uses-permission android:name="android.permission.INTERNET" /><!-- SD卡读取权限,用户写入离线定位数据--><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
  3. 初始化LocationClient类
    Context需要时全进程有效的Context,推荐用getApplicationConext获取全进程有效的Context。

    public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();...@Overrideprotected void onCreate(Bundle savedInstanceState) {   ...    //设置定位及其图标    Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.pointer),100,100,true);    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);    MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,bitmapDescriptor);    mBaiduMap.setMyLocationEnabled(true);    mBaiduMap.setMyLocationConfigeration(config);    //定位按钮    mToggleButton = (ToggleButton) findViewById(R.id.button_center);    mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类    mLocationClient.registerLocationListener( myListener );    //注册监听函数    initLocation();    //开始定位    mLocationClient.start();
  4. 配置定位SDK参数
    设置定位参数包括:定位模式(高精度定位模式、低功耗定位模式和仅用设备定位模式),返回坐标类型,是否打开GPS,是否返回地址信息、位置语义化信息、POI信息等等。

    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有效时按照1S/1次频率输出GPS结果    option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”    option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到    option.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死    option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集    option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要    mLocationClient.setLocOption(option);}
  5. 实现BDLocationListener接口
    BDLocationListener为结果监听接口,异步获取定位结果

    public class MyLocationListener implements BDLocationListener {    @Override    public void onReceiveLocation(BDLocation location) {        //Receive Location        StringBuffer sb = new StringBuffer(256);        ...        //显示新位置        if(mToggleButton.isChecked()){            makeUseOfNewLocation();        }        sb.append("time : ");        sb.append(location.getTime());        sb.append("\nerror code : ");        sb.append(location.getLocType());        sb.append("\nlatitude : ");        sb.append(location.getLatitude());        sb.append("\nlontitude : ");        sb.append(location.getLongitude());        sb.append("\nradius : ");        sb.append(location.getRadius());        if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果            sb.append("\nspeed : ");            sb.append(location.getSpeed());// 单位:公里每小时            sb.append("\nsatellite : ");            sb.append(location.getSatelliteNumber());            sb.append("\nheight : ");            sb.append(location.getAltitude());// 单位:米            sb.append("\ndirection : ");            sb.append(location.getDirection());// 单位度            sb.append("\naddr : ");            sb.append(location.getAddrStr());            sb.append("\ndescribe : ");            sb.append("gps定位成功");        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果            sb.append("\naddr : ");            sb.append(location.getAddrStr());            //运营商信息            sb.append("\noperationers : ");            sb.append(location.getOperators());            sb.append("\ndescribe : ");            sb.append("网络定位成功");        } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果            sb.append("\ndescribe : ");            sb.append("离线定位成功,离线定位结果也是有效的");        } else if (location.getLocType() == BDLocation.TypeServerError) {            sb.append("\ndescribe : ");            sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");        } else if (location.getLocType() == BDLocation.TypeNetWorkException) {            sb.append("\ndescribe : ");            sb.append("网络不同导致定位失败,请检查网络是否通畅");        } else if (location.getLocType() == BDLocation.TypeCriteriaException) {            sb.append("\ndescribe : ");            sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");        }        sb.append("\nlocationdescribe : ");        sb.append(location.getLocationDescribe());// 位置语义化信息        List<Poi> list = location.getPoiList();// POI数据        if (list != null) {            sb.append("\npoilist size = : ");            sb.append(list.size());            for (Poi p : list) {                sb.append("\npoi= : ");                sb.append(p.getId() + " " + p.getName() + " " + p.getRank());            }        }        Log.i("BaiduLocationApiDem", sb.toString());    }}
  6. 当ToggleButton选中地图自动显示当前位置

    private void makeUseOfNewLocation(){    MyLocationData.Builder builder = new MyLocationData.Builder()            .direction(mCurrentRotation)            .latitude(MyLatLng.latitude)            .longitude(MyLatLng.longitude);    mBaiduMap.setMyLocationData(builder.build());    //MapStatus使居中    MapStatus mapStatus = new MapStatus.Builder().target(MyLatLng).build();    MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);    mBaiduMap.setMapStatus(mapStatusUpdate);}
  7. 当滑动地图时设置Toggle Button不选中

    mMapView.getMap().setOnMapTouchListener(new BaiduMap.OnMapTouchListener() {        @Override        public void onTouch(MotionEvent motionEvent) {            switch (motionEvent.getAction()){                case MotionEvent.ACTION_MOVE:                    mToggleButton.setChecked(false);                    break;                default:                    break;            }        }    });

利用磁、加速度传感器显示朝向

  1. 获取传感器管理器,注册\注销磁、加速度传感器

    @Overrideprotected void onCreate(Bundle savedInstanceState) {    ...    //磁、加速度传感器    msensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);    mMagneticSensor = msensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);    mAccelerometerSensor = msensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    ...}@Overrideprotected void onResume() {    super.onResume();    ...    msensorManager.registerListener(mSensorEventListener, mMagneticSensor, SensorManager.SENSOR_DELAY_GAME);    msensorManager.registerListener(mSensorEventListener,mAccelerometerSensor,SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onPause() {    super.onPause();    ...    msensorManager.unregisterListener(mSensorEventListener);}
  2. 处理传感器数据

    private SensorEventListener mSensorEventListener = new SensorEventListener() {    float[] accValues = null;    float[] magValues = null;    @Override    public void onSensorChanged(SensorEvent event) {        switch (event.sensor.getType()) {            case Sensor.TYPE_ACCELEROMETER:                // do something about values of accelerometer                accValues = event.values.clone();                break;            case Sensor.TYPE_MAGNETIC_FIELD:                // do something about values of magnetic field                magValues = event.values.clone();                break;            default:                break;        }        float[] R = new float[9];        float[] values = new float[3];        if(accValues != null && magValues != null){            //计算朝向            SensorManager.getRotationMatrix(R, null, accValues, magValues);            SensorManager.getOrientation(R, values);            mCurrentRotation = (float) Math.round(Math.toDegrees(values[0])*100)/100;            //更新            if(MyLatLng!=null){                MyLocationData.Builder builder = new MyLocationData.Builder()                        .direction(mCurrentRotation)                        .latitude(MyLatLng.latitude)                        .longitude(MyLatLng.longitude);                mBaiduMap.setMyLocationData(builder.build());            }        }    }    @Override    public void onAccuracyChanged(Sensor sensor, int accuracy) {}};    

POI检索

  1. 创建POI检索实例,POI检索监听者(这里只在地图上显示前10个搜索结果)

    private void POISearch(){    //创建POI检索实例    mPoiSearch = PoiSearch.newInstance();    //POI检索监听者    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){        public void onGetPoiResult(PoiResult result){            List<PoiInfo> list = result.getAllPoi();            if(list == null){                Toast.makeText(getApplicationContext(),"你搜索的结果为空",Toast.LENGTH_SHORT).show();                return;            }            //获取POI检索结果            mBaiduMap.clear();            //列表大小为1页容量即10            for(int i=0;i<list.size();i++){                LatLng point = list.get(i).location;                //这里有从A-J共10个marker图标,通过图片名获取                String icon_name="icon_mark"+(char)(97+i);                //构建Marker图标                BitmapDescriptor bitmap = BitmapDescriptorFactory                        .fromResource(getDrawableResource(icon_name));                //额外信息打包,包括地址和电话                Bundle bundle = new Bundle();                bundle.putString("address",list.get(i).address);                bundle.putString("phoneNum",list.get(i).phoneNum);                //构建MarkerOption,用于在地图上添加Marker                OverlayOptions option = new MarkerOptions()                        .position(point)                        .icon(bitmap).title(list.get(i).name).extraInfo(bundle);                //在地图上添加Marker,并显示                mBaiduMap.addOverlay(option);            }            //取消自动显示当前位置            mToggleButton.setChecked(false);            //地图显示A所在位置            MapStatus mapStatus = new MapStatus.Builder().target(list.get(0).location).build();            MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);            mBaiduMap.setMapStatus(mapStatusUpdate);        }        public void onGetPoiDetailResult(PoiDetailResult result){            //获取Place详情页检索结果        }        public void onGetPoiIndoorResult(PoiIndoorResult result){};    };    mPoiSearch.setOnGetPoiSearchResultListener(poiListener);    if(!TextUtils.isEmpty(search_keywords.getText())){        //PoiNearbySearchOption,检索方圆xx米内,默认每页10条        mPoiSearch.searchNearby((new PoiNearbySearchOption()                .location(MyLatLng).radius(10000)                .keyword(search_keywords.getText().toString()).pageNum(10)));    }}
  2. 通过图片名获取图片资源(drawable路径下,若在mipmap路径类似),使用的是反射机制,有兴趣的可以了解一下。

    public int  getDrawableResource(String imageName){    Log.v("MyDebug",imageName);    Class drawable = R.drawable.class;    try {        Field field = drawable.getField(imageName);        int resId = field.getInt(imageName);        return resId;    } catch (NoSuchFieldException e) {//如果没有在"drawable"下找到imageName,将会返回0        return 0;    } catch (IllegalAccessException e) {        return 0;    }}
  3. 点击marker显示信息

    mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {        @Override        public boolean onMarkerClick(Marker marker) {            //创建InfoWindow展示的view            TextView mark_detail = new TextView(getApplicationContext());            Bundle bundle = marker.getExtraInfo();            mark_detail.setText(marker.getTitle()+"\n地址:"+bundle.getString("address")+"\n电话:"+bundle.getString("phoneNum"));            mark_detail.setTextColor(Color.BLACK);            mark_detail.setBackgroundColor(Color.BLUE);            //定义用于显示该InfoWindow的坐标点            LatLng pt = marker.getPosition();            //创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量            InfoWindow mInfoWindow = new InfoWindow(mark_detail, pt, -100);            //显示InfoWindow            mBaiduMap.showInfoWindow(mInfoWindow);            return false;        }    });

路径追踪

  1. 构建列表存储折线点坐标

     // 构造折线点坐标List<LatLng> points = new ArrayList<LatLng>();
  2. 开始运动start后,每次获得定位结果,记录与上个点相隔的距离,计入运动总距离,将该点加入列表,使用OverlayOptions将形成的折线(Polyline)列表在地图上显示。

    public class MyLocationListener implements BDLocationListener {    @Override    public void onReceiveLocation(BDLocation location) {        //Receive Location        ...        MyLatLng = new LatLng(location.getLatitude(),location.getLongitude());        if((int)start.getTag()==1){            if(points.size()>0){                distance = distance + (float)Math.round(DistanceUtil.getDistance(MyLatLng,points.get(points.size()-1))/1000*100)/100;                distance_view.setText("距离: "+ distance +"公里");            }            points.add(MyLatLng);            if(points.size()>=2){                OverlayOptions ooPolyline = new PolylineOptions().width(10).color(Color.BLACK).points(points);                //添加在地图中                mBaiduMap.addOverlay(ooPolyline);            }        }

效果

这里写图片描述

0 0
原创粉丝点击