0

I tried many code snippets but all the example takes too much time to provide latitude and longitude .

I want to get current latitude in every 5 seconds .

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mr. Mad
  • 1,230
  • 1
  • 14
  • 28
  • 1
    google locationmanager .... it has an option to get updates at a regular interval – Kushan Oct 10 '16 at 07:22
  • http://stackoverflow.com/questions/23685072/make-a-timer-handler-to-get-current-location-every-5-seconds-in-a-fragment This might help – Dwijraj Bhattacharya Oct 10 '16 at 07:22
  • What do you mean by "takes too much time"? What kind of code snippets didn't work? If you have tried any proper code snippets then there should be no problem getting GPS location updates every 5 seconds, but getting the first one might take a while. And if you use any time interval or distance threshold limitations then of course updates only come when the conditions are met. If you are using network based location it's unlikely to change unless you move significant amounts. – Markus Kauppinen Oct 10 '16 at 09:44

4 Answers4

1

You can use this post to achieve what you are looking for. Look at the setInterval(5000) will do that

enter image description here

android_griezmann
  • 3,757
  • 4
  • 16
  • 43
1

you can check and integrate this library which is a wrapper for google play services location which also handles permissions.

google location

In this lib you can find various methods to set param

Akash Jain
  • 437
  • 2
  • 13
1

You can use FusedLocationProviderAPI to make your application location aware. Using the Google Play services location APIs, your app can request the last known location of the user's device. In most cases, you are interested in the user's current location, which is usually equivalent to the last known location of the device.

For receiving most accurate location updates you can use this solution. https://developer.android.com/training/location/index.html

Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
1

You can create service that broadcasts your latitude and longitude in every 5 seconds using LocationManager.

public class MyLocationService extends Service {

private LocationManager locationManager;
private LocationListener locationListener;

public final String APP_BROADCAST_RECEIVER = "LatLonReciever"; // You should define it somewhere else as a static. e.g within Const class 

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (locationListener == null ) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                locationIsChanged(location);
            }

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

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }
        };
    }

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5000, locationListener); // here 5000 is 5 seconds
    return START_STICKY;
}


@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void locationIsChanged(Location location){
    Intent intent = new Intent(APP_BROADCAST_RECEIVER); 
    intent.putExtra("lat", location.getLatitude());
    intent.putExtra("lon", location.getLongitude());
    sendBroadcast(intent);
    }
}

Also, don't foret to register the reciever where you want to recieve the results:

public final String APP_BROADCAST_RECEIVER = "LatLonReciever"; 

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // Here you do what you want with recieved intent that contains lat and lon
            }
        };

IntentFilter intentFilter = new IntentFilter(APP_BROADCAST_RECEIVER);
registerReceiver(broadcastReceiver, intentFilter);
SpiralDev
  • 7,011
  • 5
  • 28
  • 42