首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在循环中正确调用该方法以使用UnfoldingMap显示数据

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

Stack Overflow用户
提问于 2018-01-05 04:21:54
回答 1查看 164关注 0票数 0

我是Java和Eclipse的新手,在弄清楚如何执行createMarker指令时遇到了麻烦。下面的代码运行正常,但它的编写方式与注释中的TODO (step 3)所说的不同。

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

代码语言:javascript
复制
markers.createMarker(new SimplePointMarker(earthquake.getLocation(), earthquake.getProperties())) 

但是我得到了错误The method createMarker is undefined for the type List Marker

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

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-05 04:37:38

我假设在方法setup()的末尾有一个缺失的},在

代码语言:javascript
复制
map.addMarkers(markers);

然后,您应该能够在已经存在的for-loop中调用createMarker

请记住,createMarker接受PointFeature ( for中使用的earthquake变量)作为输入。createMarker本身返回一个可以添加到标记列表中的SimplePointMarker。所以它可能看起来像这样:

代码语言:javascript
复制
for (PointFeature earthquake : earthquakes) {
     markers.add(createMarker(earthquake));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48102988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档