Skip to content Skip to sidebar Skip to footer

Location.getlongitude() And Getlatitude() Only Updating Every 100-300 Seconds

I'm working on an application that has a feature in which GPS position is tracked to draw a map. Essentially, every second, I am saving a point (containing latitude and longitude,

Solution 1:

When creating your location request set interval and fastest interval to a smaller number. Something like this:

privateval UPDATE_INTERVAL = (30 * 1000).toLong()  /* 30 secs */privateval FASTEST_INTERVAL: Long = 10000/* 10 sec */

And then create your locationRequest:

locationRequest = LocationRequest.create()
        locationRequest.interval = UPDATE_INTERVAL
        locationRequest.fastestInterval = FASTEST_INTERVAL
        locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

Solution 2:

i have using myLocationManager.

You can change UPDATE_INTERVAL_IN_MILLISECONDS property what you want

and observe mLocationCalback for location events.

classMyLocationManagerconstructor( var context: Context  ) {

    privatevar fusedLocationClient: FusedLocationProviderClient? = nullvar locationStatus = BehaviorSubject.create<LOCATION_STATUS>()
    /**
     * Provides access to the Location Settings API.
     */var mSettingsClient: SettingsClient? = null/**
     * Stores parameters for requests to the FusedLocationProviderApi.
     */privatevar mLocationRequest: LocationRequest? = null/**
     * Callback for Location events.
     */var mLocationCallback: LocationCallback? = null/**
     * Stores the types of location services the client is interested in using. Used for checking
     * settings to determine if the device has optimal location settings.
     */privatevar mLocationSettingsRequest: LocationSettingsRequest? = null/**
     * Constant used in the location settings dialog.
     */privateval REQUEST_CHECK_SETTINGS = 0x1/**
     * The desired interval for location updates. Inexact. Updates may be more or less frequent.
     */privateval UPDATE_INTERVAL_IN_MILLISECONDS: Long = 60000privateval FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2init {
        initializeLocationManager()
    }

    privatefuninitializeLocationManager() {
        if (mSettingsClient == null) {
            mSettingsClient = LocationServices.getSettingsClient(context)
        }

        if (fusedLocationClient == null) {
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
        }

        createLocationServices()
    }

    privatefuncreateLocationServices() {
        createLocationCallback()
        createLocationRequest()
        buildLocationSettingsRequest()
    }

    companionobject {
        enumclassLOCATION_STATUS{
            REQUIRE_ACCESS_FINE_LOCATION
        }
    }

    privatefuncreateLocationRequest() {
        mLocationRequest = LocationRequest()

        // Sets the desired interval for active location updates. This interval is// inexact. You may not receive updates at all if no location sources are available, or// you may receive them slower than requested. You may also receive updates faster than// requested if other applications are requesting location at a faster interval.
        mLocationRequest?.interval = UPDATE_INTERVAL_IN_MILLISECONDS

        // Sets the fastest rate for active location updates. This interval is exact, and your// application will never receive updates faster than this value.
        mLocationRequest?.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS

        mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    }

    privatefunbuildLocationSettingsRequest() {
        val builder = LocationSettingsRequest.Builder()
        mLocationRequest?.let { builder.addLocationRequest(it) }
        mLocationSettingsRequest = builder.build()
    }

    /**
     * Requests location updates from the FusedLocationApi. Note: we don't call this unless location
     * runtime permission has been granted.
     */privatefunstartLocationUpdates(activity: Activity, justControl: Boolean = false) {
        // Begin by checking if the device has the necessary location settings.if (ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.ACCESS_FINE_LOCATION
            )
            == PackageManager.PERMISSION_GRANTED
        ) {
            mSettingsClient?.let {
                it.checkLocationSettings(mLocationSettingsRequest)
                    .addOnSuccessListener(activity) {
                        fusedLocationClient?.requestLocationUpdates(
                            mLocationRequest,
                            mLocationCallback, Looper.myLooper()
                        )
                    }.addOnFailureListener(activity) { e ->
                        val statusCode = (e as ApiException).statusCode
                        when (statusCode) {
                            LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
                                try {
                                    // Show the dialog by calling startResolutionForResult(), and check the// result in onActivityResult().if (!justControl) {
                                        val rae = e as ResolvableApiException
                                        rae.startResolutionForResult(
                                            activity,
                                            REQUEST_CHECK_SETTINGS
                                        )
                                    }

                                } catch (sie: IntentSender.SendIntentException) {
                                }

                            }
                            LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                                val errorMessage =
                                    "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."

                            }
                        }
                    }
            }
        }
    }

    /**
     * Creates a callback for receiving location events.
     */privatefuncreateLocationCallback() {
        mLocationCallback = object : LocationCallback() {
            overridefunonLocationResult(locationResult: LocationResult?) {
                super.onLocationResult(locationResult)
                locationResult?.lastLocation?.let {
                    // fetched location
                }
            }
        }
    }

    fungetLastKnowLocation(activity: Activity, justControl: Boolean = false) {
        if (ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            fusedLocationClient?.let {
                it.lastLocation
                    .addOnSuccessListener { location: Location? ->
                        location?.let {
                            // fetched last know location
                        } ?: startLocationUpdates(activity, justControl)
                    }
            }
        } else {
            locationStatus.onNext(LOCATION_STATUS.REQUIRE_ACCESS_FINE_LOCATION)
        }
    }


    privatefunstopLocationUpdates() {
        fusedLocationClient?.removeLocationUpdates(mLocationCallback)
    }


}

then add permissions

<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permissionandroid:name="android.permission.INTERNET" />

Post a Comment for "Location.getlongitude() And Getlatitude() Only Updating Every 100-300 Seconds"