Search code examples
androidgoogle-cloud-messagingandroid-maps-v2android-maps-utils

Track an user with location based App


I'm working on a Location based App. Here my requirement is I want to track a user location from my device like uber or Ly ft App. Example: If a pizza guy(User B) wants to deliver pizza to me(User A) then he can share his user id, so using that id, I can enter in my device and see his exact precise location. But I want to track him until I required so how to achieve this in code. Help me with the architecture if you have come across such scenario.

I can also achieve the above scenario, but my doubt is how can I show in map without refreshing each time when the user moves say for example two minutes once I want to check the Latitude and Longitude and update it in my map for the same user


Solution

  • 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();
            }
        }
    };