基于Android实现百度地图定位过程详解
程序员文章站
2023-12-13 23:04:40
一、问题描述
lbs位置服务是android应用中重要的功能,应用越来越广泛,下面我们逐步学习和实现lbs相关的应用如定位、地图、导航等,首先我们看如何基于百度地图实现定...
一、问题描述
lbs位置服务是android应用中重要的功能,应用越来越广泛,下面我们逐步学习和实现lbs相关的应用如定位、地图、导航等,首先我们看如何基于百度地图实现定位功能
二、配置环境
1、注册密钥:地址
2、下载定位sdk,并导入sdk如图所示:
三、编写myapplication类
编写myapplication类,为了使用方便我们可以将实现定位的方法封装的application组件中
封装下列方法
1、 获取定位信息——requestlocationinfo()
2、 通过广播发送位置信息——sendbroadcast()
3、 停止定位——stoplocationclient()
public class myapplication extends application{ public locationclient mlocationclient = null; public geofenceclient mgeofenceclient; public mylocationlistenner mylistener = new mylocationlistenner(); public static string tag = "myapplication"; private static myapplication minstance = null; @override public void oncreate(){ minstance = this; mlocationclient = new locationclient(this); /** * 项目的key,自己到官网申请 http://lbsyun.baidu.com/apiconsole/key */ mlocationclient.setak("你的应用key"); mlocationclient.registerlocationlistener(mylistener); mgeofenceclient = new geofenceclient(this); super.oncreate(); log.d(tag, "... application oncreate... pid=" + process.mypid()); } public static myapplication getinstance(){ return minstance; } /** * 停止定位 */ public void stoplocationclient(){ if (mlocationclient != null && mlocationclient.isstarted()){ mlocationclient.stop(); } } /** * 发起定位 */ public void requestlocationinfo(){ setlocationoption(); if (mlocationclient != null && !mlocationclient.isstarted()){ mlocationclient.start(); } if (mlocationclient != null && mlocationclient.isstarted()){ mlocationclient.requestlocation(); } } /** * 设置百度地图的相关参数 */ private void setlocationoption(){ locationclientoption option = new locationclientoption(); option.setopengps(true); // 打开gps option.setcoortype("bd09ll"); // 设置坐标类型 option.setservicename("com.baidu.location.service_v2.9"); option.setpoiextrainfo(true); option.setaddrtype("all"); option.setpoinumber(10); option.disablecache(true); mlocationclient.setlocoption(option); } /** * 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中 */ public class mylocationlistenner implements bdlocationlistener { @override public void onreceivelocation(bdlocation location){ if (location == null){ sendbroadcast("定位失败!"); return; } sendbroadcast(location.getaddrstr()); } public void onreceivepoi(bdlocation poilocation){ if (poilocation == null){ sendbroadcast("定位失败!"); return; } sendbroadcast(poilocation.getaddrstr()); } } /** * 得到发送广播 * @param address */ public void sendbroadcast(string address){ stoplocationclient(); intent intent = new intent(mainactivity.location_bcr); intent.putextra("address", address); sendbroadcast(intent); } }
三、 主程序mainactivity
public class mainactivity extends activity{ public static string tag = "loctestdemo"; private broadcastreceiver broadcastreceiver; public static string location_bcr = "location_bcr"; private button locbtn; private textview locinfo; private myapplication application; @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); application=(myapplication)super.getapplication(); initialize(); initializeviews(); initializelisteners(); } private void initialize(){ registerbroadcastreceiver();//注册广播 } private void initializeviews() { locbtn = (button) findviewbyid(r.id.location); locinfo = (textview) findviewbyid(r.id.location_info); } private void initializelisteners() { locbtn.setonclicklistener(new onclicklistener(){ @override public void onclick(view v){ locinfo.settext("定位中..."); //调用请求定位信息 application.requestlocationinfo(); } }); } /** * 注册一个广播,监听定位结果,接受广播获得地址信息 */ private void registerbroadcastreceiver() { broadcastreceiver = new broadcastreceiver(){ public void onreceive(context context, intent intent){ string address = intent.getstringextra("address"); locinfo.settext(address); } }; intentfilter intenttoreceivefilter = new intentfilter(); intenttoreceivefilter.addaction(location_bcr); registerreceiver(broadcastreceiver, intenttoreceivefilter); } @override protected void ondestroy(){ super.ondestroy(); unregisterreceiver(broadcastreceiver); } }
四、 androidmanifest.xml配置信息
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jereh.baidulocation" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:name="com.jereh.baidulocation.myapplication" android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.jereh.baidulocation.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > <intent-filter> <action android:name="com.baidu.location.service_v2.2" > </action> </intent-filter> </service> </application> <uses-permission android:name="android.permission.access_coarse_location" > </uses-permission> <uses-permission android:name="android.permission.access_fine_location" > </uses-permission> <uses-permission android:name="android.permission.access_wifi_state" > </uses-permission> <uses-permission android:name="android.permission.access_network_state" > </uses-permission> <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" /> <uses-permission android:name="android.permission.mount_unmount_filesystems" > </uses-permission> <uses-permission android:name="android.permission.read_logs" > </uses-permission> <uses-permission android:name="android.permission.vibrate" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.write_settings" /> </manifest>
推荐阅读
-
基于Android实现百度地图定位过程详解
-
基于Android实现百度地图定位过程详解
-
Android 百度地图定位实现仿钉钉签到打卡功能的完整代码
-
Android百度地图开发之在定位图层显示他人位置(代码实现)
-
基于Android的百度地图实现输入地址返回经纬度信息
-
在Android项目里如何调用基于百度地图API实现定位
-
Android 百度地图定位实现仿钉钉签到打卡功能的完整代码
-
Android调用百度地图Web端接口,实现百度定位、导航
-
Android学习-- 基于位置的服务 LBS(基于百度地图Android SDK)--定位SDK
-
Android Studio开发(五)使用百度地图API实现实时定位