公司項目有用到百度地圖,登入的時候需要定位一次,擷取登入的位址資訊,在手機無法連接配接外網的情況,也就無法通路百度定位伺服器的時候,定位的回調函數要30秒以上才能傳回結果,于是去仔細查百度api,發現沒有設定回調函數時間的方法或者屬性.
解決辦法就是把回調函數參數指派給執行個體變量
/**
* 實作實位回調監聽,如果通路百度定位伺服器成功就會把結果指派給location
* 否則這個location為null
*/
public class mylocationlistener implements bdlocationlistener {
@override
public void onreceivelocation(bdlocation db) {
location=db;
}
}
開啟一個延時2.5秒的線程,線上程中判斷回調函數有沒有調用......如果手機能連接配接外網并且能成功通路百度伺服器的話,2秒左右能傳回結果
//延遲2.5秒執行locationresult()
//正常情況下通路百度定位伺服器隻需要2秒
new handler().postdelayed(new runnable() {
public void run() {
if(null==location){//為空表示百度sdk定位失敗
}else{//定位成功
}
}, 2500);
完整代碼如下:
package com.baidu.baidulocationdemo;
import com.baidu.location.bdlocation;
import com.baidu.location.bdlocationlistener;
import com.baidu.location.locationclient;
import com.baidu.location.locationclientoption;
import android.os.bundle;
import android.os.handler;
import android.app.activity;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.textview;
public class mainactivity extends activity {
public locationclient locationclient;
private bdlocation location;
private textview mlocationresult;
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mlocationresult=(textview) findviewbyid(r.id.location_result);
findviewbyid(r.id.start_location).setonclicklistener(clicklistener);
findviewbyid(r.id.stop_location).setonclicklistener(clicklistener);
locationclientoption option = new locationclientoption();
option.setcoortype("bd09ll");//設定坐标類型
locationclient = new locationclient(this);
locationclient.registerlocationlistener(new mylocationlistener());
locationclient.setlocoption(option);
private onclicklistener clicklistener=new onclicklistener(){
@override
public void onclick(view v) {
switch (v.getid()) {
case r.id.start_location:
locationclient.start();
//延遲2.5秒執行locationresult()
//正常情況下通路百度定位伺服器隻需要2秒
new handler().postdelayed(new runnable() {
@override
public void run() {
locationresult();
}
}, 2500);
break;
case r.id.stop_location:
distory();
}
};
/**
* 實作實位回調監聽,如果通路百度定位伺服器成功就會把結果指派給location
* 否則這個location為null
*/
public class mylocationlistener implements bdlocationlistener {
public void onreceivelocation(bdlocation db) {
location=db;
private void locationresult(){
location=new bdlocation();
//receive 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 (mlocationresult != null)
mlocationresult.settext(sb.tostring());
log.i("baidulocationapidem", sb.tostring());
distory();
private void distory(){//關閉定位
if (locationclient != null && locationclient.isstarted()) {
locationclient.stop();
}
效果圖如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI1gTOwMzN1YTM3IDMxUTMwIzLcRXZu5ibkN3Yuc2bsJmLn1Wavw1LcpDc0RHaiojIsJye.jpg)
推薦下自己建立的android qq群:202928390 歡迎大家的加入.