nihal srivastava
nihal srivastava

Reputation: 45

I want to add marker from arrayList on google map in the interval of 2 seconds

Actually I am getting the latitude and longitude value from the ArrayList, and I want to show these location on map in the interval of 3 seconds.

I have created a method named waitSec() and I'm calling it in displayLocation() but this is not working because this blocked the main thread so I used handler and asynctask and I found difficulties in this.

public void waitSec(){
    long start = System.currentTimeMillis();
    while(System.currentTimeMillis()<start+3000);
}

class MyAsyncTask extends AsyncTask<Integer,Integer,Void>{

    @Override
    protected Void doInBackground(Integer... integers) {

        waitSec();
        return null;
    }


    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Log.e("TAG", "onPostExecute: we have waited 3 seconds" );
        displayCurrentLocation();

    }
 }


private void displayCurrentLocation() {

    for(int i=0;i<trackObjectList.size();i++){

        //Log.e("TAG", "displayCurrentLocation:   "+trackObjectList.get(i).getLatitude() );

        if(trackObjectList.size()>0)
            latLng = new LatLng(Double.parseDouble(trackObjectList.get(i).getLatitude())
                ,Double.parseDouble(trackObjectList.get(i).getLongitude()));
        mMap.addMarker(new MarkerOptions().position(latLng)
                .title("Here")
                .icon(BitmapDescriptorFactory.fromBitmap(getSmallerSize(R.drawable.green_dot_th))));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15f));
        //waitSec();

    }
}

I expected that after every 2-3 seconds the markers will add on google map but it is not adding in this interval. It shows all the latlng marks on google map.

Upvotes: 0

Views: 164

Answers (3)

akshay_shahane
akshay_shahane

Reputation: 4643

You can easily achieve this using RxJava

  Observable.from(listOfItems)
                .zipWith(Observable.interval(2, TimeUnit.SECONDS), (item, notUsed) -> item)
                .subscribe(
                 // add marker addition logic here 
                        );

Upvotes: 1

bahij zeineh
bahij zeineh

Reputation: 228

you have a for loop in displayCurrentLocation which is displaying everything everytime the asyncTask.onPostExecute() runs.

try the following:

private int objectsSize = trackObjectList.size()-1;//initialise this somewhere appropriate

private void displayCurrentLocation() 
{
    int i=objectsSize--;
    //Log.e("TAG", "displayCurrentLocation: "+trackObjectList.get(i).getLatitude() );

    if(i>=0)
    {
        latLng = new LatLng(Double.parseDouble(trackObjectList.get(i).getLatitude())
                ,Double.parseDouble(trackObjectList.get(i).getLongitude()));
        mMap.addMarker(new MarkerOptions().position(latLng)
                .title("Here")
                 .icon(BitmapDescriptorFactory.fromBitmap(getSmallerSize(R.drawable.green_dot_th))));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15f));
    }
}

Upvotes: 0

Hitesh Kushwah
Hitesh Kushwah

Reputation: 1361

You need to animate your marker when create--

private void animateMarker(final Marker marker) {
    final Handler handler = new Handler();

    final long startTime = SystemClock.uptimeMillis();
    final long duration = 300; // ms

    Projection proj = mMap.getProjection();
    final LatLng markerLatLng = marker.getPosition();
    Point startPoint = proj.toScreenLocation(markerLatLng);
    startPoint.offset(0, -10);
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);

    final Interpolator interpolator = new BounceInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - startTime;
            float t = interpolator.getInterpolation((float) elapsed / duration);
            double lng = t * markerLatLng.longitude + (1 - t) * startLatLng.longitude;
            double lat = t * markerLatLng.latitude + (1 - t) * startLatLng.latitude;
            marker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                // Post again 16ms later (60fps)
                handler.postDelayed(this, 16);
            }
        }
    });
}

Upvotes: 0

Related Questions