关于百度地图实现定位的两种方式(baiduSDK 和 Service+BaiduSDK)

来源:互联网 发布:淘宝宝贝命名规则 编辑:程序博客网 时间:2024/05/18 19:45

这几天呢我写个项目里面涉及到百度地图定位的功能,那我就简单的说一下他的实现原理:


1:基础的百度地图定位

想要实现这个功能其实很简单,只需要把在百度地图上的包导到android Studio的libs里面,添加依赖库,不过他的前提是需要在百度地图开发者平台注册一个应用信息,得到他的SHA1,接着在清单文件里面添加相应的权限,还有在开发者平台里面注册App的key一定要添加进去,这些工作做好之后,就可以在后台实现代码,首先在setContentView(),前面初始化SDK,具体方法如下:



(1.在百度地图API开发选项中打开API控制台

  1. 创建应用:完成之后提交

我的应用找到查看应用

将包导入到libs下

在build.gradle中添加依赖库,并build一下

在清单文件里添加权限,写入KEY

在主窗体的setContentView前初始化SDK



2:使用百度地图自动在后台实现定位

既然说道在后台实现功能那就少不了Service了,那我们就说一下在后台实现定位的实现方法:

首先上一种方法的前(1、2、3、4、5、6)种方法是必不可少的,那么就要说Service了,Service分为两种,一种是本地的,另一种是远程的,初始化百度SDk需要创建一个类继承Applocation这个类

public class App extends Application {    @Override    public void onCreate() {        super.onCreate();        SDKInitializer.initialize(getApplicationContext());    }}
然后再清 单文件注册一下,当然因为在Service里面实现的,serVice也是要注册一下的

    <application        android:name=".application.App"        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".activity.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".service.MyService" />        <meta-data            android:name="com.baidu.lbsapi.API_KEY"            android:value="6ZPUV7we7RxdINuISYFrzqw9cxgLgnSK" />        <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote" />    </application>

接着就要先开始布局了

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical">    <TextView        android:id="@+id/txtShow"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="定位"        android:textSize="17sp" /></LinearLayout>
在Activity里面实例化控件就不说了,就先从Service说起ba,定义一个类,继承自Service

public class MyService extends Service {    private String city = "";  //用来接收城市的字符串    public static Handler handler;//定位是耗时操作,Service里面不能做耗时操作,所以用Handler来接收    private MyBind bind = new MyBind();    private LocationClient client;   //客户端定位    @Override    public void onCreate() {        //调用方法        getLocation();        super.onCreate();    }    //得到用户的具体位置    private void getLocation() {        //        client = new LocationClient(getApplicationContext());        //给客户端注册监听        client.registerLocationListener(new MyLocationLitener());        //新建一个客户端位置选项        LocationClientOption option = new LocationClientOption();        //设置打开GPS        option.setOpenGps(true);        //设置地区类型        option.setAddrType("all");        option.setCoorType("bd0911");        //设置范围        option.setScanSpan(600);        //客户端得到这个选项        client.setLocOption(option);        //打开客户端        client.start();    }    //此方法是为了监听用户操作    private class MyLocationLitener implements BDLocationListener {        @Override        public void onReceiveLocation(BDLocation bdLocation) {            if (bdLocation == null) {                Toast.makeText(getApplicationContext(), "网络无法连接,定位失败!", Toast.LENGTH_SHORT).show();                return;            }            //使用StringBuffer来接收中国内的城市            StringBuffer buffer = new StringBuffer(256);            //判断GPS/NETWORK 来接收得到的城市名称            if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {                buffer.append(bdLocation.getCity());            } else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {                buffer.append(bdLocation.getCity());            }                //判断得到的城市是否为空            if (!TextUtils.isEmpty(buffer.toString())) {                //如果handler不为空的话,就用city来接收buffer得到的城市                if (handler != null) {                    city = buffer.toString();                    //handler发送消息到Activity                    handler.sendEmptyMessage(0);                }            }        }    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return bind;    }    @Override    public boolean onUnbind(Intent intent) {        return super.onUnbind(intent);    }    @Override    public void onDestroy() {        super.onDestroy();    }    //新建一个内部类,继承自Binder目的是为了让Activity能够绑定Service里面的信息    public class MyBind extends Binder {        //继承Binder所要实现的方法        public String getCity() {            return city;        }    }}
Activity里的具体实现

//定位数据类    private MyService.MyBind bind;
绑定Service需要使用Intent来接收传过来的
 /**     * 绑定service,实现定位     */    private void location() {        Intent intent = new Intent(getActivity(), MyService.class);        getActivity().bindService(intent, connection, Context.BIND_AUTO_CREATE);        MyService.handler = handler;    }    private ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            bind = (MyService.MyBind) service;        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };
这些写完之后就要使用Handler来给主线程发送消息

private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);                         if (bind != null) {                    txtShow.setText(bind.getCity());                }             }    };
那么使用BaiduSDK在后台实现自动定位就完成了o((≧▽≦o)








1 0
原创粉丝点击