How to get My Location changed event with Google Maps android API v2?

后端 未结 3 1868
攒了一身酷
攒了一身酷 2020-11-27 14:58

How to get My Location changed event with Google Maps android API v2?

In v1 you can do the next in order to handle location changed event:



        
3条回答
  •  醉话见心
    2020-11-27 15:31

    I encountered the same problem... my solution by now:

    Create a Thread, that asks a Handler to get the position a few times. You need to use a Handler, because the getMyLocation method can only be called from GUI Thread:

    private class MyLocationThread extends Thread{
            @Override
            public void run() {
                int loops = 0;
                // we give the location search a minute 
                while(loops < 60){
                    // we have to try it over a handler, because getMyLocation() has to be called from GUI Thread -_-
                    _getMyLocationHandler.sendEmptyMessage(0);
                    if(isInterrupted()){
                        return;
                    }
                    // take a short nap before next try
                    try {Thread.sleep(1000);} catch(Exception e){}
                    loops++;
                }
    
            }
        }
    

    Here's what the Handler does:

    private Handler _getMyLocationHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {
                if(getMap().isMyLocationEnabled() && getMap().getMyLocation() != null){
                    _locationWatcher.interrupt();
                    drawCurrentImagePositions();
                }
            }
        };
    

提交回复
热议问题