Thread Tools
This thread is privately moderated by Jack Crossfire, who may elect to delete unwanted replies.
Mar 29, 2015, 11:52 PM
Registered User
Jack Crossfire's Avatar
Thread OP
Discussion

Android GPS rides again


So the very last attempt before going to sleep got GPS to go again. Some
years ago, Google dropped support for the LocationManager & switched to the
FusedLocationApi, which was far more complex. The general idea is in this
incomplete Main class:


Code:
public class Main implements 
	OnConnectionFailedListener, 
	ConnectionCallbacks, 
	GoogleApiClient.ConnectionCallbacks, 
	GoogleApiClient.OnConnectionFailedListener,
	LocationListener, 
	GpsStatus.Listener
{
	
	public GoogleApiClient mGoogleApiClient;

// start GPS
	void init()
	{
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(LocationServices.API)
                        .build();

                mGoogleApiClient.connect();
	}


// get location immediately
	void polling()
	{
            if(mGoogleApiClient.isConnected())
			{
				Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
			}
	}

// manually stop GPS
	void close()
	{
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,
                        this);
                mGoogleApiClient.disconnect();
	}


// callbacks from the location API
	@Override
	public void onConnected(Bundle connectionHint) {
// Only getting 1hz
        final int locationInterval = 1000;

        LocationRequest mLocationRequest=new LocationRequest();
        mLocationRequest.setInterval(locationInterval);
        mLocationRequest.setFastestInterval(locationInterval);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest,
                this);
	}

	@Override
	public void onGpsStatusChanged(int event) {
	
	}

// callback when location is ready.  This is more efficient than polling, but 
// doesn't happen at a known time
	@Override
	public void onLocationChanged(Location location) {
	}
}

Another step backward & another step forward. If anyone can make a serial
port require 2 API's & 10 million lines of code, it's the Goog.
Sign up now
to remove ads between posts


Quick Reply
Message:
Thread Tools