如何迭代Java中的Marker列表?

时间:2015-10-02 11:30:42

标签: java google-maps google-maps-api-3 processing

List<Marker> markers = new ArrayList<Marker>();
 List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this, earthquakesURL);



  for( PointFeature earthquake: earthquakes){



    SimplePointMarker marker = new SimplePointMarker(earthquake.getLocation());
    map.addMarker(marker); // <---- I used this method from Unfolding Maps library, 
                         //does this method add elements to the array list?

    }

...  // Code
...


for(Marker markerz : markers){
        System.out.println("Hello");
    Object magn=markerz.getProperty("magnitude");

    float magnitude= Float.parseFloat(magn.toString());
    System.out.println(magnitude);

    if( magnitude<=4.0){
        markerz.setColor(color(0, 0, 255));

    }
    else  if((float) magnitude<=4.9){
        markerz.setColor(color(255, 255 , 0 ));

    }
    else  if((float) magnitude>=5.0){
        markerz.setColor(color(255, 0 , 0 ));}

    else{
            markerz.setColor(color(150,150,150));
        }

    }

我正在进行这个在线课程,他们要求我在地图上制作标记,然后根据地震的大小改变标记颜色。我试图迭代标记这是一个arrayList我认为我的条件语句肯定存在一些问题。然后我在循环中附加了一个打印行语句来检查控件是否进入循环内部。但是“你好”也没有打印出来。如何迭代循环?我有另一个ArrayList类型的PointList。我可以迭代这个吗?

2 个答案:

答案 0 :(得分:2)

for(Marker markerz : markers)

是迭代List的好方法。但是您迭代的List对象“markers”可能是空的,因此循环不会执行。确保在//代码部分中添加一些Marker对象。

答案 1 :(得分:-1)

for (PointFeature earthquake : earthquakes) {
               SimplePointMarker earthquakeMarker = createMarker(earthquake);
               markers.add(earthquakeMarker);//this adds to the list <marker>
         }
           map.addMarkers(markers); // this adds to the markers to the map

//下面是createMarker()

private SimplePointMarker createMarker(PointFeature feature)
    {
        int yellow = color(255, 255, 0); // using PApplet to get int from RGB to put into Marker
        int blue = color(0, 0, 255);
        int red = color(255, 0, 0);
        float earthquakeRadius = 0; //initialize radius
        int earthquakecolor = 0; //initialize color
        Object magObj = feature.getProperty("magnitude");
        float mag = Float.parseFloat(magObj.toString()); // get magnitude from earthquake object

        if (mag < 4.0) {earthquakeRadius = 8; earthquakecolor = blue;}
        else if (mag <5.0) {earthquakeRadius = 15; earthquakecolor = yellow;}
        else {earthquakeRadius = 25; earthquakecolor = red;}

        Location earthquakeLocation = feature.getLocation(); // get locations
        SimplePointMarker earthquakeMarker = new SimplePointMarker(earthquakeLocation);
        earthquakeMarker.setRadius(earthquakeRadius); // sets radius
        earthquakeMarker.setColor(earthquakecolor);
        return earthquakeMarker;
        // finish implementing and use this method, if it helps.
        //return new SimplePointMarker(feature.getLocation());
    }