skip to Main Content

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


  1. you can make a global variable long toastTime=0 and in your onLocationChanged put toastTime+=10 and replace if statement in your code

    if(toastTime%1000==0)/*you can change 1000 to another value if you want*/{
    toastTime=0;  
    Toast.makeText(MapAcvitiy.this,address,Toast.LENGTH_SHORT).show();   
    }

    not perfect but it will work fine

    Login or Signup to reply.
  2. Remove the toast or you have to use onLocationUpdateRemove() and you can display address in TextView it will updating when onLocationUpdate() called

    Login or Signup to reply.
  3. could add the global variable isToastShow to make the toast only once.

        boolean isToastShow =false;
    
        if(!isToastShow){
            Toast.makeText(MapAcvitiy.this, address, Toast.LENGTH_SHORT).show();
            isToastShow = true;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search