Fast and Frequent location Update in android..?

拈花ヽ惹草 提交于 2019-12-07 17:24:55

问题


public void getUserLocation() {

    Location location;
    TextView lon = (TextView) findViewById(R.id.textView2);
    TextView lat = (TextView) findViewById(R.id.textView3);
    boolean GpsEnable = false, NetworkEnabled = false;
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);




    // String locationProvider = LocationManager.GPS_PROVIDER;

    GpsEnable = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    NetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    // locationManager.requestLocationUpdates(locationProvider,0,0,locationListner);

    if (!GpsEnable && !NetworkEnabled) {
        Toast.makeText(getBaseContext(), "No Provider Availabe",
                Toast.LENGTH_SHORT);

    } else {

        if (NetworkEnabled)
            Toast.makeText(getBaseContext(), "Network Provider Available",
                    Toast.LENGTH_SHORT);
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, this);

        if (locationManager != null) {
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            lon.setText("Latitude" + location.getLatitude());
            lat.setText("Longitude " + location.getLongitude());
        }

        if (GpsEnable) {

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, this);

            if (locationManager != null) {

                location = locationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location != null) {
                    lon.setText("Latitude" + location.getLatitude());
                    lat.setText("Longitude " + location.getLongitude());

                }
            }
        }
    }
}

I had done both with GPS and network provider.. I want to know how we get exact current location in Google maps? Is there any way or algorithm by which i can get longitude and latitude of my location using internally Google maps? Thanks in Advance


回答1:


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

Initial Setup Here

Alternative for LocationUpdate

Hope this could help...:)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/20278683/fast-and-frequent-location-update-in-android

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