百度地图定位
百度地图定位
huiyun-yoyo 发表于11个月前
百度地图定位
  • 发表于 11个月前
  • 阅读 11
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

摘要: 将百度地图用到jar包和.so文件放到jniLibs文件下。对请求地址进行封装,一个Observable和一个Subscriber。在Subscribe中对UI进行操作。这里的地址就定位一次就不在定位了。
//Observable 被观察者。
public class LocationObservable {

    private static LocationService locationService;
    private static String result;

    public static void getLocation(final RxAppCompatActivity context, final MyApplication application, Subscriber subscriber) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(final Subscriber<? super String> subscriber) {
                locationService = application.locationService;
                locationService.registerListener(new BDLocationListener() {
                    @Override
                    public void onReceiveLocation(BDLocation location) {
                        if (null != location && location.getLocType() != BDLocation.TypeServerError) {
                            result = location.getCity();
                            if (location.getLocType() == BDLocation.TypeServerError) {
                                result = "服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因";
                            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                                result = "网络不同导致定位失败,请检查网络是否通畅";
                            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                                result = "无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机";
                            }
                        } else {
                            result = "";
                        }
                        subscriber.onNext(result);
                        subscriber.onCompleted();
                        locationService.stop();
                    }
                });
                locationService.setLocationOption(locationService.getDefaultLocationClientOption());
                locationService.start();
            }
        })
                .compose(context.<String>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(context.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(subscriber);
    }
}

// Subscriber观察者
public class LocationSubscribe extends Subscriber<String> {
    private Context context;

    public LocationSubscribe(Context context) {
        this.context = context;
    }

    @Override
    public void onCompleted() {


    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(final String s) {
        if (s != null && !s.equals(""))
            new AlertDialog.Builder(context).setTitle("当前定位").setMessage(s).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SPUtil.setGPSCity(s);
                }
            }).show();
    }

}


//在Activity中订阅,在Activity中对权限进行检查
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<>();
             //定位权限为必须权限,用户如果禁止,则每次进入都会申请
            // 定位精确位置
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }

            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
}

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // TODO Auto-generated method stub
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

LocationObservable.getLocation(this,(MyApplication)getApplication(),new LocationSubscribe(this));


//在MyApplication中的初始化SDK
        locationService = new LocationService(getApplicationContext());
        mVibrator = (Vibrator) getApplicationContext().getSystemService(VIBRATOR_SERVICE);
        SDKInitializer.initialize(getApplicationContext());





共有 人打赏支持
粉丝 2
博文 42
码字总数 33793
×
huiyun-yoyo
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: