Adding Markers in Maps during app is running unsuccessful

I have been trying to implement markers in my google maps activity while the application runs. There are some pre-defined "Location" objects, and there is a marker at the "current location" of the user.

Using a SeekBar, I'm trying to change the range of the current location in which the markers of the location objects should be displayed, but on changing the SeekBar, nothing happens (no markers get added, there's just one at the current location). Apologies for the unwanted extra code, I believe the most important parts to look at are onConnected(), onSeekBarChangedListener() and onMapReady()

This is my MapsActivity.java:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.
OnConnectionFailedListener, OnSeekBarChangeListener {

    private GoogleApiClient googleApiClient;
    private SeekBar rangeSeekBar;
    private GoogleMap mMap;
    private Intent intent;
    private Location currentLocation;
    private Location[] locations;
    private double range;

    Location locationOne;
    Location locationTwo;
    Location locationThree;
    Location locationFour;
    Location locationFive;
    Location locationSix;
    Location locationSeven;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        locationOne = new Location("");
        locationTwo = new Location("");
        locationThree = new Location("");
        locationFour = new Location("");
        locationFive = new Location("");
        locationSix = new Location("");
        locationSeven = new Location("");

        locationOne.setLatitude(10.1399);
        locationOne.setLongitude(76.1784);

        locationTwo.setLatitude(10.2244);
        locationTwo.setLongitude(76.1978);

        locationThree.setLatitude(9.6175);
        locationThree.setLongitude(76.4301);

        locationFour.setLatitude(9.5987);
        locationFour.setLongitude(76.3116);

        locationFive.setLatitude(9.6175);
        locationFive.setLongitude(76.4301);

        locationSix.setLatitude(9.6737);
        locationSix.setLongitude(76.5610);

        locationSeven.setLatitude(15.5152);
        locationSeven.setLongitude(73.8565);

        locations = new Location[]{
            locationOne,
            locationTwo,
            locationThree,
            locationFour,
            locationFive,
            locationSix,
            locationSeven
        };

        rangeSeekBar = (SeekBar)findViewById(R.id.rangeSeekBar);
        rangeSeekBar.setProgress(0);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        googleApiClient.disconnect();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng india = new LatLng(20.5937, 78.9629);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(india, 5));

        if(googleApiClient==null){
            googleApiClient  = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        }

        googleApiClient.connect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        if(ContextCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MapsActivity.this,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},1);
        } else {
            currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

            LatLng currentLoc = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
            mMap.addMarker(new MarkerOptions().position(currentLoc).title("Your current location"));
        }

        addingMarkersInRange(0);
    }

    public void addingMarkersInRange(double range){
        for(int i=0;i<locations.length;i++){
            if(currentLocation.distanceTo(locations[i])<range) {
                LatLng tempLatLng = new LatLng(locations[i].getLatitude(), locations[i].getLongitude());
                mMap.addMarker(new MarkerOptions().position(tempLatLng));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(tempLatLng));
            }
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        addingMarkersInRange(rangeSeekBar.getProgress()*700000);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
} 


ANSWERS:


Your addinMarkersInRange() method is only adding markers for LatLngs in locations array. So, you need to add to locations array before invoking addingMarkersInRange. But, since you cannot add to an array, please change locations to a List and you should be good to go.

You are not setting listener on SeekBar object. Please set OnSeekBarChangeListener like so in your onCreate():

// code you already have
rangeSeekBar = (SeekBar)findViewById(R.id.rangeSeekBar);
rangeSeekBar.setProgress(0);

// register event listener here
rangeSeekBar.setOnSeekBarChangeListener(this);

I think you have issue in addingMarkersInRange(). But let proceed in a way, First set listener to seekbar. Then set max value of seekbar. Now initially call addingMarkersInRange(1) instead of passing zero.

Now while seek bar change make use of isfromUser flag in onProgress change.

And remove that onCameraChange() & place it outside of for loop. And use latlongbound to animate camera zoom level.

Now run and check any change made on UI ...!



 MORE:


 ? toggle button in google map API
 ? Google Places Autocomplete boundaries doesn't always work
 ? I want google maps marker to stay in the centre of the map while camera position can change iOS SDK
 ? Google maps - keep center when zooming
 ? Type 'Promise<void>' is not assignable to type 'Marker'. Property '_objectInstance' is missing in type 'Promise<void>'. Ionic 2
 ? Google Map is not showing on live site using Avada theme
 ? Adding google tiles with R
 ? Chrome navigator.geolocation.getCurrentPosition() error 403
 ? How to move inside google maps only with two fingers?
 ? React Native: Alert message doesn't work when called frequently