如何在循环中正确调用方法以使用UnfoldingMap显示数据

时间:2018-01-04 20:21:54

标签: java methods

我是Java和Eclipse的新手,我在查明如何执行createMarker指令时遇到了麻烦。下面的代码运行正常,但它的编写方式与评论中的TODO (step 3)表示应该编写的方式不同。

例如,当我尝试在循环中调用createMarker方法时,我这样写了:

markers.createMarker(new SimplePointMarker(earthquake.getLocation(), earthquake.getProperties())) 

但我收到错误The method createMarker is undefined for the type List Marker

有人能告诉我在循环中调用createMarker的正确方法吗?我已经尝试过课堂论坛和之前的Stack Overflow问题,但我还没有找到答案。

public class EarthquakeCityMap extends PApplet {

    // You can ignore this.  It's to keep eclipse from generating a warning.
    private static final long serialVersionUID = 1L;

    private static final boolean offline = false;

    // Less than this threshold is a light earthquake
    public static final float THRESHOLD_MODERATE = 5;
    // Less than this threshold is a minor earthquake
    public static final float THRESHOLD_LIGHT = 4;

    // The map
    private UnfoldingMap map;

    //feed with magnitude 2.5+ Earthquakes
    private String earthquakesURL = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.atom";


    public void setup() {
        size(950, 600, OPENGL);

        if (offline) {
            map = new UnfoldingMap(this, 200, 50, 700, 500, new MBTilesMapProvider(mbTilesString));
            earthquakesURL = "2.5_week.atom";   // Same feed, saved Aug 7, 2015, for working offline
        }
        else {
            map = new UnfoldingMap(this, 200, 50, 700, 500, new Google.GoogleMapProvider());
            //map = new UnfoldingMap(this, 200, 50, 700, 500, new Microsoft.HybridProvider());
            // IF YOU WANT TO TEST WITH A LOCAL FILE, uncomment the next line
            //earthquakesURL = "2.5_week.atom";
        }

        map.zoomToLevel(2);
        MapUtils.createDefaultEventDispatcher(this, map);   

        // The List you will populate with new SimplePointMarkers
        List<Marker> markers = new ArrayList<Marker>();

        //Use provided parser to collect properties for each earthquake
        //PointFeatures have a getLocation method
        List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this, earthquakesURL);

        //TODO (Step 3): Add a loop here that calls createMarker (see below) 
        // to create a new SimplePointMarker for each PointFeature in 
        // earthquakes.  Then add each new SimplePointMarker to the 
        // List markers (so that it will be added to the map in the line below)
        for (PointFeature earthquake : earthquakes) {
                markers.add(new SimplePointMarker(earthquake.getLocation(), earthquake.getProperties()));
        }

        // Add the markers to the map so that they are displayed
        map.addMarkers(markers);

    /* createMarker: A suggested helper method that takes in an earthquake 
     * feature and returns a SimplePointMarker for that earthquake
     * 
     * In step 3 You can use this method as-is.  Call it from a loop in the 
     * setup method.  
     */
    private SimplePointMarker createMarker(PointFeature feature)
    {  

        // Create a new SimplePointMarker at the location given by the PointFeature
        SimplePointMarker marker = new SimplePointMarker(feature.getLocation());

        Object magObj = feature.getProperty("magnitude");
        float mag = Float.parseFloat(magObj.toString());        

        // Finally, return the marker
        return marker;
    }

1 个答案:

答案 0 :(得分:0)

我认为方法}末尾缺少setup()

之后
map.addMarkers(markers);

然后,您应该可以在现有的createMarker循环中调用for

请注意,createMarker需要PointFeatureearthquake中使用的for变量作为输入。 createMarker本身会返回一个SimplePointMarker,可以将其添加到标记列表中。 所以看起来可能类似于:

for (PointFeature earthquake : earthquakes) {
     markers.add(createMarker(earthquake));
}