第一步:
有号的登录,没号的注册。
下载相关Sdk:点击打开链接 只下载AndroidSdk定位!!!这里就不图片展示了,发链接咯~
-----------------------将libs下的文件尽数复制到你的工程目录下---------------
获取密钥 注册过的用户就可以创建应用了
这里需要的是一个SHA1
创建完应用以后就可以获取到key值(以后会用到)
第二步:配置环境
配置server:
- <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">
- </service>
声明权限:
- <!-- 这个权限用于进行网络定位-->
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
- <!-- 这个权限用于访问GPS定位-->
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
- <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
- <!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
- <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
- <!-- 用于读取手机当前的状态-->
- <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
- <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <!-- 访问网络,网络定位需要上网-->
- <uses-permission android:name="android.permission.INTERNET" />
- <!-- SD卡读取权限,用户写入离线定位数据-->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
配置appkey
- <meta-data
- android:name="com.baidu.lbsapi.API_KEY"
- android:value="key" />
第三步:代码书写
可以放在点击事件里面----点击获取位置 也可以配置在程序入口 详细的注释在代码中----------
-
-
- mLocationClient = new LocationClient(MyApplication.getContext());
- mLocationClient.registerLocationListener(new BDLocationListener() {
-
- @Override
- public void onReceiveLocation(BDLocation location) {
-
- 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());
- if (location.getLocType() == BDLocation.TypeGpsLocation) {
- sb.append("\nspeed : ");
- sb.append(location.getSpeed());
- sb.append("\nsatellite : ");
- sb.append(location.getSatelliteNumber());
- sb.append("\nheight : ");
- sb.append(location.getAltitude());
- sb.append("\ndirection : ");
- sb.append(location.getDirection());
- sb.append("\naddr : ");
- sb.append(location.getAddrStr());
- sb.append("\ndescribe : ");
- sb.append("gps定位成功");
-
- } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
- sb.append("\naddr : ");
- sb.append(location.getAddrStr());
-
- sb.append("\noperationers : ");
- sb.append(location.getOperators());
- sb.append("\ndescribe : ");
- sb.append("网络定位成功");
- } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
- sb.append("\ndescribe : ");
- sb.append("离线定位成功,离线定位结果也是有效的");
- } else if (location.getLocType() == BDLocation.TypeServerError) {
- sb.append("\ndescribe : ");
- sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
- } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
- sb.append("\ndescribe : ");
- sb.append("网络不同导致定位失败,请检查网络是否通畅");
- } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
- sb.append("\ndescribe : ");
- sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
- }
- sb.append("\nlocationdescribe : ");
- sb.append(location.getLocationDescribe());
- List<Poi> list = location.getPoiList();
- if (list != null) {
- sb.append("\npoilist size = : ");
- sb.append(list.size());
- for (Poi p : list) {
- sb.append("\npoi= : ");
- sb.append(p.getId() + " " + p.getName() + " "
- + p.getRank());
- }
- }Log.d("<span style="font-family: Arial, Helvetica, sans-serif;">location</span>",sb.toString);
-
- menuweizhi.setText(location.getLocationDescribe());
- menuweizhitime.setText(location.getTime());
-
- }
- });
-
- mLocationClient.start();
-
- LocationClientOption option = new LocationClientOption();
-
- option.setLocationMode(LocationMode.Hight_Accuracy);
- option.setCoorType("bd09ll");
- option.setScanSpan(0);
- option.setIsNeedAddress(true);
- option.setOpenGps(true);
- option.setLocationNotify(true);
- option.setIsNeedLocationDescribe(true);
- option.setIsNeedLocationPoiList(true);
- option.setIgnoreKillProcess(false);
- option.SetIgnoreCacheException(false);
- option.setEnableSimulateGps(false);
- mLocationClient.setLocOption(option);
-
- mLocationClient.requestLocation();
运行完总会效果不尽如人意什么的 这里可以参照返回码
61 : GPS定位结果,GPS定位成功。
62 : 无法获取有效定位依据,定位失败,请检查运营商网络或者wifi网络是否正常开启,尝试重新请求定位。
63 : 网络异常,没有成功向服务器发起请求,请确认当前测试手机网络是否通畅,尝试重新请求定位。
65 : 定位缓存的结果。
66 : 离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果。
67 : 离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果。
68 : 网络连接失败时,查找本地离线定位时对应的返回结果。
161: 网络定位结果,网络定位定位成功。
162: 请求串密文解析失败,一般是由于客户端SO文件加载失败造成,请严格参照开发指南或demo开发,放入对应SO文件。
167: 服务端定位失败,请您检查是否禁用获取位置信息权限,尝试重新请求定位。
502: key参数错误,请按照说明文档重新申请KEY。
505: key不存在或者非法,请按照说明文档重新申请KEY。
601: key服务被开发者自己禁用,请按照说明文档重新申请KEY。
602: key mcode不匹配,您的ak配置过程中安全码设置有问题,请确保:sha1正确,“;”分号是英文状态;且包名是您当前运行应用的包名,请按照说明文档重新申请KEY。
501~700:key验证失败,请按照说明文档重新申请KEY。
布局其实没什么 3个控件
- <com.hao.news.view.MyTextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="点击获取最新地理位置"
- android:id="@+id/menu_tianqi"
- android:textSize="18sp"
- android:layout_marginTop="40dp"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="25dp"
- android:text="位置显示"
- android:id="@+id/menu_weizhi"
- android:textColor="#ff0000"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="获取时间"
- android:id="@+id/menu_weizhi_time"
- android:layout_marginLeft="120dp"
- android:textColor="#00ffff"
- />