欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

百度地图定位

程序员文章站 2022-06-10 12:44:04
...
//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号和大体定位时间到[email protected],会有人追查原因";
                            } 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());





转载于:https://my.oschina.net/huiyun/blog/788284