Hey my code is requesting location every momement and on the onLocationChanged method I have a toast which shows the GPS address, the problem is that onLocationChanged is getting called every moment while I don’t want a Toast of the location every moment because I end up being spammed with Toasts, is there a way to change this to something that could be more nice for the user?
public void onLocationChanged(Location location) {
mMap.clear();
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
Context context;
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (listAddresses != null && listAddresses.size() > 0)
{
if (listAddresses.get(0).getThoroughfare() != null)
{
address += listAddresses.get(0).getThoroughfare() + " ";
}
if (listAddresses.get(0).getLocality() != null)
{
address += listAddresses.get(0).getLocality() + " ";
}
if (listAddresses.get(0).getPostalCode() != null)
{
address += listAddresses.get(0).getPostalCode() + " ";
}
if (listAddresses.get(0).getAdminArea() != null)
{
address += listAddresses.get(0).getAdminArea();
}
Toast.makeText(MapAcvitiy.this, address, Toast.LENGTH_SHORT).show();
Log.i("Address", address);
}
} catch (IOException e) {
e.printStackTrace();
}
}
Another part of my code is this where I create a locationManager and set the requestLocationUpdates to request the location ever moment.
if (Build.VERSION.SDK_INT < 23) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
}else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mMap.clear();
LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
}
}
3
Answers
you can make a global variable
long toastTime=0
and in your onLocationChanged puttoastTime+=10
and replace if statement in your codenot perfect but it will work fine
Remove the toast or you have to use
onLocationUpdateRemove()
and you can display address in TextView it will updating whenonLocationUpdate()
calledcould add the global variable isToastShow to make the toast only once.