Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为循环执行提供内部延迟_Android_Google Maps_Delay - Fatal编程技术网

Android 为循环执行提供内部延迟

Android 为循环执行提供内部延迟,android,google-maps,delay,Android,Google Maps,Delay,我有一个for循环,在这个循环中我调用了一个方法来显示映射中的poi。现在我要做的是按顺序显示每个poi。我的意思是,现在for循环在一段时间内完成,最后显示最终输出 但我希望它是有序的,这意味着每次循环迭代时,我应该能够看到这些地质点从一个地方移动到另一个地方的动画 这是我的密码 for (i=currentPOIindex;i<arraypoi.size();i++) { poi = arraypoi.get(i); latitude

我有一个for循环,在这个循环中我调用了一个方法来显示映射中的poi。现在我要做的是按顺序显示每个poi。我的意思是,现在for循环在一段时间内完成,最后显示最终输出

但我希望它是有序的,这意味着每次循环迭代时,我应该能够看到这些地质点从一个地方移动到另一个地方的动画

这是我的密码

 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();
    }
}

for(i=currentPOIindex;i您应该在for循环中执行以下操作:

  final Handler handler = new Handler();
   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
        handler.postDelayed(new Runnable() {
        @Override
      public void run() {
     //Do something after 100ms
       moveToPOI(i);

   }
  }, 100/*This is Delay Value in Milli Second set which suit for your /*);  


    }
final Handler=new Handler();

对于(i=currentPOIindex;i您可以使用处理程序postDelayed和Runnable来执行实际操作,在Runnable run()中,您删除该Runnable的回调,并将其再次发布到处理程序。如果您正在为完成的操作获取一些回调,则可以使用该回调再次触发Runnable

Handler handler = new Handler()
handler.postDelayed(myRunnable, MY_DELAY);
Runnable myRunnable = new Runnable() {
public void run() {
//do the stuff

// Check if another loop sequence is needed and start runnable again
handler.removeCallbacks(myRunnable);
handler.postDelayed(myRunnable, MY_DELAY); }

最后我找到了答案。@Niko和@Herry,我不知道这是否与您的代码相似

这是找我的钱

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


但是谢谢你的帮助。这对我很有用。

Thread.sleep()?Thread.sleep(),破坏了我的用户界面,并且只在地理点高亮显示后才使用。谢谢,我将尝试此代码。这里缺少分号(;)吗?哦,是的,因为我在后面的评论)我们必须添加;。这与我以前的代码的行为方式相同。我将延迟增加到1000,但它仍在以拉伸的方式移动。正在发生的是函数moveToPOI(I);在for循环结束时被调用,我的意思是在完成整个for循环后,现在当它进入函数movetoPOI时,它抛出java.lang.IndexOutOfBoundsException:无效索引46,行AudioMapOverLay=(AudioMapOverLay)mapOverlays.get(currentPOI)的大小为46;好的,您希望它是UI线程还是nomral线程就足够了?无论如何,我会尝试bothas,根据我的理解,我应该将for循环块放入run()中吗?moveToPOI()应该在run方法中调用。首先创建项,然后在循环外启动runnable。您还需要将runnable中的成员整数传递给moveToPOI(索引).public void animateTo(GeoPoint point,java.lang.Runnable Runnable)。我建议使用此选项,因为您在此处传递的Runnable将在动画完成时被调用,因此您可以在此处启动新动画。您不需要在此处进行postDelayed调用。
 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);
}