Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I use for locationlistener, using all the same permissions as you.</p> <pre><code>private void startLocationListener() { userPosition.setPosition(myPosition); // Get the location manager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. if(currLocation != null) { boolean better = isBetterLocation(location, currLocation); if(better) { currLocation.set(location); myPosition = new LatLng(currLocation.getLatitude(), currLocation.getLongitude()); userPosition.setPosition(myPosition); } } else { currLocation = location; myPosition = new LatLng(currLocation.getLatitude(), currLocation.getLongitude()); userPosition.setPosition(myPosition); } } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; locationManager.requestLocationUpdates(GPSPROVIDER, 5000, 0, locationListener); locationManager.requestLocationUpdates(NETPROVIDER, 30000, 0, locationListener); } protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta &gt; TIME; boolean isSignificantlyOlder = timeDelta &lt; -TIME; boolean isNewer = timeDelta &gt; 0; // If it's been more than two minutes since the current location, use the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta &gt; 0; boolean isMoreAccurate = accuracyDelta &lt; 0; boolean isSignificantlyLessAccurate = accuracyDelta &gt; 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer &amp;&amp; !isLessAccurate) { return true; } else if (isNewer &amp;&amp; !isSignificantlyLessAccurate &amp;&amp; isFromSameProvider) { return true; } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload