公司项目有用到百度地图,登录的时候需要定位一次,获取登录的地址信息,在手机无法连接外网的情况,也就无法访问百度定位服务器的时候,定位的回调函数要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() {
- @Override
- 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;
- @Override
- 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();
- break;
- }
- }
- };
- /**
- * 实现实位回调监听,如果访问百度定位服务器成功就会把结果赋值给location
- * 否则这个location为null
- */
- public class MyLocationListener implements BDLocationListener {
- @Override
- public void onReceiveLocation(BDLocation db) {
- location=db;
- }
- }
- private void locationResult(){
- if(null==location){//为空表示百度sdk定位失败
- 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();
- }
- }
- }
效果图如下:
推荐下自己创建的android QQ群:202928390 欢迎大家的加入.
时间: 2025-01-09 07:02:37