Android 百度sdk5.0定位

來源:互聯網
上載者:User

標籤:

在開發中可能 會用到諸如“定位出當前所在城市,所在位置的經緯度”等功能。一次性的定位肯能定位不成功,可能經緯度未讀出來,也可能是地址資訊或者城市資訊未讀出來。此時就需對定位後拿到的資訊做判斷,加入沒有拿到理想的資料,則再定位一次,知道定位成功並且拿到理想的資料。

代碼如下

public class LocationCurrentCity {    private LocationClient locationClient;    private Context context;    public LocationCurrentCity(Context context) {        this.context = context;        locationClient = new LocationClient(context);        // 設定定位條件        LocationClientOption option = new LocationClientOption();        option.setOpenGps(false); // 是否開啟GPS        option.setCoorType("bd09ll"); // 設定傳回值的座標類型。        option.setIsNeedAddress(true);// 返回的定位結果包含地址資訊        option.setAddrType("all");        option.setLocationMode(LocationMode.Hight_Accuracy);        // option.setPriority(LocationClientOption.MIN_SCAN_SPAN_NETWORK); //        // 設定定位優先順序        // option.setProdName("LocationDemo");        // //設定產品線名稱。強烈建議您使用自訂的產品線名稱,方便我們以後為您提供更高效準確的定位服務。        option.setScanSpan(5000); // 設定定時定位的時間間隔。單位毫秒        locationClient.setLocOption(option);        // 註冊位置監聽器        locationClient.registerLocationListener(listener);    }    BDLocationListener listener = new BDLocationListener() {        @Override        public void onReceiveLocation(BDLocation location) {            if (location == null||location.getCity()==null) {                locationClient.requestLocation();            }else{                if(callback != null){                    callback.callback(location);                }                stop();                DevLog.e("location:" + location.getLatitude() + ":"                        + location.getLongitude());            }                    }    };    public interface LocationCallback {        public void callback(BDLocation location);    }    private LocationCallback callback;    public void setLocationCallback(LocationCallback callback){        this.callback = callback;    }    public void start(){        locationClient.start();        locationClient.requestLocation();    }    public void stop(){        locationClient.stop();        locationClient.unRegisterLocationListener(listener);    }}

 

Android 百度sdk5.0定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.