【百度地图】定位

本文介绍了如何使用百度地图定位SDK进行位置获取。通过创建LocationClient对象并注册自定义的MyLocationListener监听器来实现定位功能。文中详细展示了如何设置定位参数、启动及停止定位服务,并提供了关键代码示例。

1. 定义对象

LocationClient mLocationClient = null;
 BDLocationListener myListener = new MyLocationListener();
 LocationData locData = null;

 

2. 新建MyLocationListener类 实现BDLocationListener接口

public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null)
                return;
            mLocationClient.stop();
StringBuffer sb = new StringBuffer(256);
            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());
            sb.append("\nlocation.getAltitude():" + location.getAltitude());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }

            System.out.println(sb.toString());
        }

        public void onReceivePoi(BDLocation poiLocation) {
            // 将在下个版本中去除poi功能
        }
    }

3.

public void MyLocation() {
        mLocationClient = new LocationClient(this.getApplicationContext());
        locData = new LocationData();
        mLocationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);// 设置发起定位间隔时间
        mLocationClient.setLocOption(option);
        mLocationClient.start();
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.requestLocation();
        } else
            Log.d("LocSDK3", "locClient is null or not started");
    }

转载于:https://www.cnblogs.com/4evercai/p/3670030.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值