我的Android開發之路——百度地圖開源工具擷取定位資訊

來源:互聯網
上載者:User

標籤:tap   裝置   star   定位   art   需要   操作   post   state   

  定位技術在現在的行動裝置上是必不可少的,許多app都會使用定位功能。

  通常定位方式有兩種:GPS定位;網路定位。

  Android系統對這兩種定位方式都提供了相應的API支援,但是因為google的網路服務在中國不可訪問,所以在中國很少用到Android原生定位API,而是使用一些國內第三方公司的SDK(如百度、高德)。這次學習的就是百度的LBS(需要提前申請API Key,下載相應的包)。

lbsyun.baidu.com

  1、在Gradle檔案(Module:app)android標籤下中添加jniLibs目錄,這裡是專門存放so檔案。(在選擇android 的選項下面顯示的jniLibs 目錄和選擇 project 選項下面的libs 其實是一個)

    sourceSets{        main{            jniLibs.srcDirs=[‘libs‘]        }    }

 

  然後Sync同步更新gradle就會多出jniLibs檔案夾,然後把下載好的壓縮包檔案複製到當前檔案夾下,在Sync一下即可使用LBS服務了。

 

  2、在AndroidManifest.xml中申明許可權,在application標籤中添加API Key,再註冊百度SDK中的一個服務

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <uses-permission android:name="android.permission.WAKE_LOCK"/>

  

<!-- 百度地圖 -->        <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote">        </service>        <meta-data android:name="com.baidu.lbsapi.API_KEY"                   android:value="UIrhfCXVUrf8ITGCFexZhqPFmutb72TQ"/>

  完成這些操作後就能使用LBS服務了。

  3、LBS的基礎使用方法

  ①首先需要LocationClient執行個體,LocationClient的構建函數接收一個Context參數,這裡調用getApplicationContext()方法擷取一個全域的Context參數並傳入。

public class MainActivity extends AppCompatActivity {    public LocationClient mLocationClient;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mLocationClient=new LocationClient(getApplicationContext());    }}

  ②然後調用LocationClient的registerLocationListener()方法來註冊一個定位監聽器,當擷取到位置資訊的時候,就會回調這個定位監聽器。

     其中定位監聽器可以自訂調用不同的資訊(經度、緯度、定位時間、定位類型燈)

public class MainActivity extends AppCompatActivity {    public LocationClient mLocationClient;    public BDLocationListener myListener = new MyLocationListener();    public class MyLocationListener implements BDLocationListener{        @Override        public void onReceiveLocation(BDLocation bdLocation) {        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLocationClient=new LocationClient(getApplicationContext());        mLocationClient.registerLocationListener(myListener);    }}

  在監聽器中自訂(如下可以得到經緯度和定位方式的資訊)

    public class MyLocationListener implements BDLocationListener{        @Override        public void onReceiveLocation(BDLocation bdLocation) {            StringBuilder sb=new StringBuilder();            sb.append("緯度:").append(bdLocation.getLatitude()).append("\n");            sb.append("經度:").append(bdLocation.getLongitude()).append("\n");            sb.append("定位方式:");            if (bdLocation.getLocType()==BDLocation.TypeGpsLocation){                sb.append("GPS");            }else if (bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){                sb.append("網路");            }        }    }

  ③最後調用LocationClient的start()方法即可開始定位。

        mLocationClient.start();

  ④在布局添加textview用於顯示出定位資訊。整個MainActivity代碼如下:

public class MainActivity extends AppCompatActivity {public LocationClient mLocationClient;    public BDLocationListener myListener = new MyLocationListener();    private TextView positontext;    public class MyLocationListener implements BDLocationListener{        @Override        public void onReceiveLocation(BDLocation bdLocation) {            StringBuilder sb=new StringBuilder();            sb.append("緯度:").append(bdLocation.getLatitude()).append("\n");            sb.append("經度:").append(bdLocation.getLongitude()).append("\n");            sb.append("定位方式:");            if (bdLocation.getLocType()==BDLocation.TypeGpsLocation){                sb.append("GPS");            }else if (bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){                sb.append("網路");            }            positontext.setText(sb);        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        positontext=(TextView) findViewById(R.id.position_text);        mLocationClient=new LocationClient(getApplicationContext());        mLocationClient.registerLocationListener(myListener);        mLocationClient.start();    }}

  使用模擬器運行一下,完成簡易的定位功能

 

 

  4、設定LocationClient的屬性

  在start()方法之前,建立LocationClientOption的執行個體對象,通過這個對象設定一些屬性,最後通過LocationClient的setLocOption()方法調用這些設定。

 

        mLocationClient.registerLocationListener(myListener);        LocationClientOption option=new LocationClientOption();        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);        //可選,預設高精度,設定定位元模式,高精度,低功耗,僅裝置        option.setCoorType("bd09ll");        //可選,預設gcj02,設定返回的定位結果座標系        int span = 1000;        option.setScanSpan(span);        //可選,預設0,即僅定位一次,設定發起定位請求的間隔需要大於等於1000ms才是有效        option.setIsNeedAddress(true);        //可選,設定是否需要地址資訊,預設不需要。設定為true後,可以再listener中通過getCountry()、getProvice()、getCity()等方法得到具體的地區街道資訊        option.setOpenGps(true);        //可選,預設false,設定是否使用gps                mLocationClient.setLocOption(option);                mLocationClient.start();

 

我的Android開發之路——百度地圖開源工具擷取定位資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.