文档章节

百度地图定位

huiyun-yoyo
 huiyun-yoyo
发布于 2016/11/15 17:40
字数 448
阅读 15
收藏 0
//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());





© 著作权归作者所有

共有 人打赏支持
huiyun-yoyo
粉丝 1
博文 43
码字总数 33793
作品 0
瑞安
程序员
私信 提问

暂无文章

为什么日志要private static final

private static final Log logger = LogFactory.getLog(SpringProperties.class); 对于使用PMD的任何人来说,如果日志记录器没有声明为静态的和final的,那么这个博客的标题就会出现在PMD错误...

stys35
18分钟前
1
0
基于深度学习模型Wide&Deep的推荐

本实验选用数据为UCI开源数据集,仅用于学习,请勿商用) Wide&Deep推荐算法出自一篇论文《Wide&Deep Learning for RecommenderSystems》,Wide&Deep由两部分组成,分别是Wide和Deep。先来说...

阿里云官方博客
22分钟前
1
0
列出文件目录结构

以下代码可快速列出所有文件夹,子文件夹下文件 用tree /f显示为树形结构 mypath = wsh.exec("cmd /c tree /f " & Chr(34) & mypath & Chr(34)).StdOut.ReadAll 用 dir显示为长文件名结构 my...

tedzheng
23分钟前
1
0
8.05-Win10 的一些个人设置

1、隐藏 Cortana搜索框 右击任务栏 ——> 找到Cortana ——> 里面有三个选项(隐藏;显示Cortana图标;显示搜索框) ——> 选中自己所需样式 2、显示“我的电脑”等按钮 桌面右键 ——> 个性...

静以修身2025
25分钟前
1
0
windows 下 es安装ik后报错无法启动

1、没有安装分词器时可以正常启动 2、在安装分词器后就报错了 错误信息:Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "D...

zhu_kai1
29分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部