Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android My Location-Fix

When my map activity is called I make a call in the onCreate to addUserMapPoint. This function contains two instances where I try to get the location information using myOverlay.getMyLocation. On the initial load of this activity the result of the first attempt returns a null GeoPoint and after the main UI thread completes the second attempt located in the listener thread of myOverlay.runOnFirstFix(new Runnable()… is call after a second and does contain a GeoPoint that does contain a lat and lon. The call inside this listener function does appear to put the dot on the map and the line mapController.animateTo(gp) does move the map to my location. My app has a refresh button that when clicked fires off this activity again. I need the lat and lon in order to fetch location data from another service. After the refresh, the second time through the map activity code I was expecting the first call to myOverlay.getMyLocation() would now be able to get the GeoPoint, but it is still null.

If I’m not able to get the GeoPoint by this first call to myOverlay.getMyLocation then how can I pass the lat and lon value from the second call found in the myOverlay.runOnFirstFix(new Runnable()… thread. You will notice that I have been trying to add the lat and lon to MyApp which is helper bean class but the lat and lon in this class is null even after the refresh. If I manually set a lat and lon manually in the addUserMapPoint function the first time the activity is accessed these values are retained. I’m guessing that this is because it is being set on the main UI thread.

public class MapActivity extends com.google.android.maps.MapActivity {
    private MapView mapView = null;
    private MapController mapController = null;
    private MyLocationOverlay myOverlay = null;

    public static MyApp app;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        app = (MyApp) getApplicationContext();

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();

        List<Overlay> mapOverlays = mapView.getOverlays();
        mapOverlays.clear();

        addUserMapPoint(mapView);

        if (!app.isLocServOff()) {
            //map other points – service call to get items from our service near lat and lon
            addOtherMapPoints(mapOverlays);

        } else {
            Toast.makeText(app.getApplicationContext(),"Current location could not be found.",Toast.LENGTH_LONG).show();
        }

    }

    private void addUserMapPoint(MapView mapView){
            myOverlay = new MyLocationOverlay(app.getApplicationContext(), mapView);
            myOverlay.disableCompass();
        myOverlay.enableMyLocation();


            if(app.getMyLat()==null||app.getMyLon()==null){
                GeoPoint gp = myOverlay.getMyLocation();
                if(gp!=null){
                    app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                    app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                    app.setLocServOff(false);
                }else{
                    app.setLocServOff(true);
                }
        }


        myOverlay.runOnFirstFix(new Runnable() {   
                public void run() {     
                    GeoPoint gp = myOverlay.getMyLocation();    
                    if(gp!=null){
                        app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                        app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                        app.setLocServOff(false);   
                        mapController.animateTo(gp);
                    }else{
                        app.setLocServOff(true);
                    }
                }    
            });

        mapView.getOverlays().add(myOverlay);
    }   

}

Your help is being requested for the following question. How can I get a GeoPoint that contains a lat and lon in the main UI thread or how can I pass these values from GeoPoint I am able to get from the myOverlay.runOnFirstFix(new Runnable()… thread?

If you are going to suggest that I use Handler or runOnUiThread please provide code example that passes the lat and lon back to something that can be used by the main UI thread/map view. I have tried things like the following code that did not produce the desired outcome. I was able to get the toast message to show up, but was not able to get the lat and lon passed in a way I could use.

    final Handler handler = new Handler();   
    myOverlay.runOnFirstFix(new Runnable() {                     
        @Override public void run() {

            handler.post(new Runnable() {                                   
                @Override public void run() {

                        GeoPoint gp = myOverlay.getMyLocation();    
                        if(gp!=null){
                            app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                            app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                            app.setLocServOff(false);   
                            mapController.animateTo(gp);                                

                        }else{
                            app.setLocServOff(true);
                        }

                    //Toast.makeText(getApplicationContext(),"wowoowowowoowoowowow",Toast.LENGTH_LONG).show();                      
                    }                                  
                });

            }                
        }); 

I've also used code like the following to get the lat and lon and it works, but because the current location would sometimes be a different lat and lon than whas was being returned becuase for example I could not get a gps signal but yet an old value was returned. I added checks to see if the lat/lon data was older than 2 minutes, but I still could not match up the most recent lat and lon with that that is returned by myOverlay.getMyLocation.

    LocationManager locMgr = (LocationManager)appcontext.getSystemService(Context.LOCATION_SERVICE);
    MyLocationListener locLstnr = new MyLocationListener();

    //fetch current location for current location 
    locMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER, locLstnr, appcontext.getMainLooper());  
like image 342
ItBHarsH Avatar asked Dec 19 '12 16:12

ItBHarsH


1 Answers

Bellow you can find some examples on how to get the current location in the UI thread, but first of all, some background information.

GPS may take some time (15 seconds to 1 minute) to get the first fix after the request for new location is made. This is the reason you your first attempt to get it from myOverlay fails, and only after the first fix you can get the value.

During this blackout period you can use getLastKnownLocation() to get the last good known GPS location if you are in a hurry. If not availble it returns null

The code:

Last Known Location

LocationManager locMgr=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc != null){
    //we have a valid location. Check location date
}

Requesting a Single Location Update

LocationManager locMgr=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, appcontext.getMainLooper);

Requesting a Continuous Location Update

LocationManager locMgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
//use 0 for minDistance and minDistance between updates if you need the maximum update frequency.
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, minDistance, minTime, locationListener);

Location Listener for Single and Continuous position update

This is the last piece of code, and is the place where you get the new fresh locations requested above.

When a new location that match your request critirea defined above is retrieved by GPS, this listener is immediately called, unless you device is busy doing something else that can't be interrupted (i.e. callback is on a paused thread or that hit a lock).

From within the onLocationChanged() you can set any class level filed as appropriate. If you registered the listener from the UI thread, then this will be running running on the UI.

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location fix) {
        fix.setTime(fix.getTime() + timeZoneOffset); //Add Timezone offset if needed
        //here you have a fresh new location in fix...
        //You can set the value of any class level field from here
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

Regards.

like image 195
Luis Avatar answered Sep 27 '22 17:09

Luis