Fast and Frequent location Update in android..?

余生颓废 提交于 2019-12-05 20:07:12

I have Followed this Tutorial for Fast Update and Initial SetUp for GoogleMap v2

Initial Setup Here

Alternative for LocationUpdate

Hope this could help...:)

public void retriveLocation(){
    try {
        String locCtx = Context.LOCATION_SERVICE;
        LocationManager locationmanager = (LocationManager) context.getSystemService(locCtx);           
         Criteria criteria = new Criteria();
         criteria.setAccuracy(Criteria.ACCURACY_FINE);
         criteria.setAltitudeRequired(false);
         criteria.setBearingRequired(false);
         criteria.setPowerRequirement(Criteria.POWER_LOW);
         String provider = locationmanager.getBestProvider(criteria, true);
         locationmanager.requestLocationUpdates(provider, 0, 0, this);
    } catch (Exception e) {         
    }

}

Hope this code can be useful for retrieving fast location updates.

Here is the code that I basically use to get a constant location signal using Google Play Services.

public class MyActivity
extends
    Activity
implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener,
    LocationListener
{
    private LocationClient locClient;
    private LocationRequest locRequest;
    // Flag that indicates if a request is underway.
    private boolean servicesAvailable = false;


    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        // Check that Google Play services is available
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            servicesAvailable = true;
        } else {

            servicesAvailable = false;
        }

        if(locClient == null) {
            locClient = new LocationClient(this, this, this);
        }

        if(!locClient.isConnected() || !locClient.isConnecting())
        {
            locClient.connect();
        }

        // Create the LocationRequest object
        locRequest = LocationRequest.create();
        // Use high accuracy
        locRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locRequest.setInterval(INTERVAL);
        locRequest.setFastestInterval(INTERVAL);
    }

    @Override
    public void onLocationChanged( Location location )
    {
        // DO SOMETHING WITH THE LOCATION
    }

    @Override
    public void onConnectionFailed( ConnectionResult arg0 )
    {
    }

    @Override
    public void onConnected( Bundle arg0 )
    {
        // Request location updates using static settings
        locClient.requestLocationUpdates(locRequest, this);
    }

    @Override
    public void onDisconnected()
    {
        if(servicesAvailable && locClient != null) {
            //
            // It looks like after a time out of like 90 minutes the activity
            // gets destroyed and in this case the locClient is disconnected and
            // calling removeLocationUpdates() throws an exception in this case.
            //
            if (locClient.isConnected()) {
                locClient.removeLocationUpdates(this);
            }
            locClient = null;
        }
    }
}

This is the crux of it anyway and I culled this from other sources so I can't really claim it but there it is.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!