我有一个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();
}
}
我尝试使用Thread.sleep(long ms);但这不是解决方案,任何人都可以告诉我该怎么做?
答案 0 :(得分:2)
你必须在你的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 /*);
}
答案 1 :(得分:1)
您可以使用Handler 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); }
答案 2 :(得分:0)
最后我想出了答案。 @Niko和@Herry,我不知道这是否与你的代码类似。
这是我的改变,
notification = new Runnable() {
public void run() {
testgeo();
}
};
handler.postDelayed(notification, 1500);
其中testge0()是,
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);
}
但是谢谢你的帮助。这对我有用