Search code examples
androidgoogle-mapsdelay

Giving delay inside for loop execution


I have a for loop, inside which I'm calling a method to show the poi's in map. Now what I am trying to do is to show each poi in a sequence. I mean now the for loop completes at a stretch and at last is presented with the final output.

But I want it to be in order, that mean each time for loop iterates I should be able to see the animation where these geopoints are moving from place to place.

Here is my code,

 for (i=currentPOIindex;i<arraypoi.size();i++) {
            poi = arraypoi.get(i);
            latitude = poi.getLatitude().toString();
            longitude = poi.getLongitude().toString();
            placename = poi.getPlaceName().toString();
            mapdescription = poi.getPoiDecsription().toString();
            lat = Double.parseDouble(latitude);
            lng = Double.parseDouble(longitude);
            //use the poiIndex to identify which poi is highlighted
            itemizedOverlay.poiIndex = i;
            geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename), fitTextToMapCalloutDescription("hello"));
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);
           //this method is meant to move to each geopint one by one. 
           //And I am trying to give a delay before this method is called
            moveToPOI(i);
        }


  public void moveToPOI(int currentPOI) {
    try {
        AudioMapOverLay overlay = (AudioMapOverLay) mapOverlays.get(currentPOI);
        poi = arraypoi.get(currentPOI);
        latitude = poi.getLatitude().toString();
        longitude = poi.getLongitude().toString();
        placename = poi.getPlaceName().toString();
        lat = Double.parseDouble(latitude);
        lng = Double.parseDouble(longitude);
        geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        overlay.onForwardOrRewind(currentPOI, overlay);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I tried using Thread.sleep(long ms); But it is not the solution, can any one tell me what to do?


Solution

  • Finally I have come up with the answer. @Niko and @Herry, I don't know if this is similar to your code.

    here is my change,

         notification = new Runnable() {
                public void run() {
                    testgeo();
                }
            };
            handler.postDelayed(notification, 1500);
    

    where testge0() is,

     private void testgeo() {
        poi = arraypoi.get(index);
        latitude = poi.getLatitude().toString();
        longitude = poi.getLongitude().toString();
        placename = poi.getPlaceName().toString();
        // mapdescription = poi.getPoiDecsription().toString();
        lat = Double.parseDouble(latitude);
        lng = Double.parseDouble(longitude);
        if (index == currentPOIindex) {
            drawable = this.getResources().getDrawable(R.drawable.poiiconactive);
            itemizedOverlay = new AudioMapOverLay(drawable, objMapView,this);
        } else {
            drawable = this.getResources().getDrawable(R.drawable.mapicon);
            itemizedOverlay = new AudioMapOverLay(drawable, objMapView,this);
        }
        itemizedOverlay.poiIndex = index;
        geopoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem = new OverlayItem(geopoint,fitTextToMapCallout(placename),
                fitTextToMapCalloutDescription("hello"));
        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);
        moveToPOI(index);
    }
    

    But thank you for your help. This is working for me