代码之家  ›  专栏  ›  技术社区  ›  Biii

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

  •  0
  • Biii  · 技术社区  · 8 年前

    我是Java和Eclipse的新手,在弄清楚如何执行 createMarker 指示下面的代码运行正常,但不是按照 TODO (step 3) 在评论中说应该写出来。

    例如,当我试图打电话给 创建标记 方法,我这样写:

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

    但我错了 The method createMarker is undefined for the type List Marker .

    谁能告诉我打电话的正确方式吗 创建标记 在循环中?我尝试过班级论坛和以前的堆栈溢出问题,但我一直没有找到答案。

    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 回复  |  直到 8 年前
        1
  •  0
  •   Stefan Freitag    8 年前

    我想有一个 } 在方法末尾 setup() 之后

    map.addMarkers(markers);
    

    那么你应该可以打电话 createMarker 在已经存在的 for -循环。

    请记住 创建标记 需要 PointFeature (the earthquake 中使用的变量 对于 作为输入。 创建标记 自身返回 SimplePointMarker 可以添加到标记列表中。 所以它可能看起来像这样:

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