百度地图LocationClient.start()方法无法重复获取经纬度

最近项目需要到离线地图,然后研究了下百度地图的定位,然后入坑了
百度地图的LocationClient.start()方法无定时获取到经纬度,使用 client.requestLocation()方法还是没卵用,百度尝试了所以的方法,还是不行;
最后,使用了最坑的方式,换位实现了:

先贴代码

 private LocationClient client;
    private LocationClientOption clientOption;
    private double mCurrentLat = 0.0;
    private double mCurrentLon = 0.0;
    private Handler handler;
    public MyLocationListenner myListener = new MyLocationListenner();
    private Timer timer;
    private TimerTask timerTask;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    //初始化参数
    @SuppressLint("HandlerLeak")
    private void init() {
        timer = new Timer();
        clientOption = new LocationClientOption();
        clientOption.setCoorType("bd0911");
        clientOption.setOpenGps(true);
        clientOption.setLocationNotify(true);
        clientOption.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startLocation();
                    }
                });
            }
        };
        timerTask = new TimerTask() {
            @Override
            public void run() {
                stopLocation();
                handler.sendEmptyMessageDelayed(1, 2500);
            }
        };
    }


    private void startLocation() {
        client = new LocationClient(getApplicationContext());
        client.setLocOption(clientOption);
        client.registerLocationListener(myListener);
        client.start();
    }

    private void stopLocation() {
        if (client != null) {
            client.setLocOption(null);
            client.unRegisterLocationListener(myListener);
            client.stop();
            client = null;
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        timer.schedule(timerTask, 2 * 1000, 5 * 1000);
    }

    @Override
    protected void onStop() {
        timer.cancel();
        handler.removeMessages(1);
        stopLocation();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        super.onDestroy();
    }

    private void printLog(String content) {
        Log.e("MainActivityprintLog", content);
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            int type = location.getLocType();
            printLog("type:" + type);
            if (location != null) {
                //定位回调
                mCurrentLat = location.getLatitude();
                mCurrentLon = location.getLongitude();
                printLog("mCurrentLat:" + mCurrentLat + "\n" + "mCurrentLon:" + mCurrentLon);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "mCurrentLat:" + mCurrentLat + "\n" + "mCurrentLon:" + mCurrentLon, Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                printLog("bdLocation is null");
            }
        }
    }

关键解释

就是在每次定位前把LocaationClient置空,然后重新new 然后start,又因为stop和start是异步执行的,所以需要handler延迟一下再start(不然不成功,试过)

	//开始定位
    private void startLocation() {
        client = new LocationClient(getApplicationContext());
        client.setLocOption(clientOption);
        client.registerLocationListener(myListener);
        client.start();
    }

//停止定位
   private void stopLocation() {
        if (client != null) {
            client.setLocOption(null);
            client.unRegisterLocationListener(myListener);
            client.stop();
            client = null;
        }
    }
//延迟代码
  handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startLocation();
                    }
                });
            }
        };
        timerTask = new TimerTask() {
            @Override
            public void run() {
                stopLocation();
                handler.sendEmptyMessageDelayed(1, 2500);
            }
        };
发布了43 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_38355313/article/details/86631571