Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 基於Android實現百度地圖定位過程詳解

基於Android實現百度地圖定位過程詳解

編輯:關於Android編程

一、問題描述

LBS位置服務是android應用中重要的功能,應用越來越廣泛,下面我們逐步學習和實現lbs相關的應用如定位、地圖、導航等,首先我們看如何基於百度地圖實現定位功能

二、配置環境

1、注冊密鑰:地址http://developer.baidu.com/map/

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>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved