1、問題描述 |
LBS位置服務是android應用中重要的功能,應用愈來愈普遍,下面咱們逐步學習和實現lbs相關的應用如定位、地圖、導航等,首先咱們看如何基於百度地圖實現定位功能android
2、配置環境 |
一、註冊密鑰:地址http://developer.baidu.com/map/web
二、下載定位SDK,並導入SDK如圖所示:api
3、編寫MyApplication類 |
編寫MyApplication類,爲了使用方便咱們能夠將實現定位的方法封裝的Application組件中app
封裝下列方法ide
一、 獲取定位信息——requestLocationInfo()函數
二、 經過廣播發送位置信息——sendBroadCast()學習
三、 中止定位——stopLocationClient()this
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); } }
3、 主程序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); } }
4、 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>