android 百度地圖 定位功能

來源:互聯網
上載者:User

標籤:android

廢話不多說 直接建立一個新android項目:location,然後花一分鐘申請一個key,然後就是把百度定位demo抄一下就行

1:首先在AndroidManifest.xml中添加許可權

<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" />

2:在AndroidManifest.xml檔案中使用自己寫的Application 在application節點中加上一行代碼

android:name="com.baidu.baidulocationdemo.LocationApplication" 

3:在application節點上 複製如下內容

<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>
        <!-- meta-data需要寫在application中 -->
        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="xAxmR5WgrssC9ATV8gZOvq96" />

在這注意需要把換成自己申請的key

4:配置就算ok了,那麼就寫代碼麼,誰叫我們是碼農呢? 首先看下自己定義的類MyApplication.java

package com.example.location;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.Poi;import android.app.Application;import android.app.Service;import android.os.Vibrator;import android.util.Log;import android.widget.TextView;import java.util.List;public class MyApplication extends Application {    public LocationClient mLocationClient;    public MyLocationListener mMyLocationListener;    public TextView mLocationResult,logMsg;    public TextView trigger,exit;    public Vibrator mVibrator;@Overridepublic void onCreate() {super.onCreate(); mLocationClient = new LocationClient(this.getApplicationContext());        mMyLocationListener = new MyLocationListener();        mLocationClient.registerLocationListener(mMyLocationListener);        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);} /**     * 實現即時位置回調監聽     */    public class MyLocationListener implements 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){// GPS定位結果                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號和大體定位時間到[email protected],會有人追查原因");            } 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();// POI資訊            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());                }            }            logMsg(sb.toString());            Log.i("BaiduLocationApiDem", sb.toString());        }    }    /**     * 顯示請求字串     * @param str     */    public void logMsg(String str) {        try {            if (mLocationResult != null)                mLocationResult.setText(str);        } catch (Exception e) {            e.printStackTrace();        }    }}

這裡是直接從百度定位sdk demo中複製過來的,這個不需要去敲,因為它沒啥技術和邏輯可言.

5:MainActivity.java

package com.example.location;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.LocationClientOption.LocationMode;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv_locaiton_result;private LocationClient mLocationClient;private LocationMode tempMode = LocationMode.Hight_Accuracy;private String tempcoor="gcj02";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mLocationClient = ((MyApplication)getApplication()).mLocationClient;tv_locaiton_result = (TextView) findViewById(R.id.tv_locaiton_result);((MyApplication)getApplication()).mLocationResult = tv_locaiton_result;initLocation();} private void initLocation(){        LocationClientOption option = new LocationClientOption();        option.setLocationMode(tempMode);//可選,預設高精度,設定定位元模式,高精度,低功耗,僅裝置        option.setCoorType(tempcoor);//可選,預設gcj02,設定返回的定位結果座標系,        int span=5000;        option.setScanSpan(span);//可選,預設0,即僅定位一次,設定發起定位請求的間隔需要大於等於1000ms才是有效        option.setOpenGps(true);//可選,預設false,設定是否使用gps        option.setLocationNotify(true);//可選,預設false,設定是否當gps有效時按照1S1次頻率輸出GPS結果        option.setIgnoreKillProcess(true);//可選,預設true,定位SDK內部是一個SERVICE,並放到了獨立進程,設定是否在stop的時候殺死這個進程,預設不殺死        option.setEnableSimulateGps(false);//可選,預設false,設定是否需要過濾gps模擬結果,預設需要        option.setIsNeedLocationDescribe(true);//可選,預設false,設定是否需要位置語義化結果,可以在BDLocation.getLocationDescribe裡得到,結果類似於“在北京天安門附近”        option.setIsNeedLocationPoiList(true);//可選,預設false,設定是否需要POI結果,可以在BDLocation.getPoiList裡得到        mLocationClient.setLocOption(option);        mLocationClient.start();    }}

ok,定位就好了 不過這是定位擷取到地址,地圖上顯示  下次弄  



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

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.