【问题标题】:Adding markers to a unfolding map in Java在 Java 中向展开的地图添加标记
【发布时间】:2015-10-25 20:37:58
【问题描述】:

我需要添加代码以在 RSS 提要中每次地震的位置显示一个标记,并通过修改方法(并根据需要添加新的辅助方法)使每个标记的半径为 10。然后我需要根据地震的大小添加代码来设置每个标记的样式。

我知道我应该为列表特征中的每个 PointFeature 创建一个 SimplePointMarker 对象,在 setUp 方法中使标记出现在屏幕上,所以它可能涉及每个循环,但除此之外我不是确定如何编码。

展开标记包说明...http://unfoldingmaps.org/javadoc/

public class EarthquakeCityMap extends PApplet {

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

    // IF YOU ARE WORKING OFFLINE, change the value of this variable to true
    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;

    /** This is where to find the local tiles, for working without an Internet connection */
    public static String mbTilesString = "blankLight-1-3.mbtiles";

    // The map
    private UnfoldingMap map;

    //feed with magnitude 2.5+ Earthquakes
    private String earthquakesURL = "http://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());
            // 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);

        // These print statements show you (1) all of the relevant properties 
        // in the features, and (2) how to get one property and use it
        if (earthquakes.size() > 0) {
            PointFeature f = earthquakes.get(0);
            System.out.println(f.getProperties());
            Object magObj = f.getProperty("magnitude");
            float mag = Float.parseFloat(magObj.toString());
            // PointFeatures also have a getLocation method
        }

        // Here is an example of how to use Processing's color method to generate 
        // an int that represents the color yellow.  
        int yellow = color(255, 255, 0);
        int red = color(255, 0, 0);
        int blue = color(0, 0, 255); 
        //TODO: Add code here as appropriate
    }

    // A suggested helper method that takes in an earthquake feature and     
    // returns a SimplePointMarker for that earthquake
    // TODO: Implement this method and call it from setUp, if it helps
    private SimplePointMarker createMarker(PointFeature feature)
    {
        //feature.setColor(color(150, 150, 150)); 

        // finish implementing and use this method, if it helps.
        return new SimplePointMarker(feature.getLocation());

        //earthquakeMarkers = MapUtils.create
    }

    public void draw() {
        background(10);
        map.draw();
        addKey();
    }

    // helper method to draw key in GUI
    // TODO: Implement this method to draw the key
    private void addKey() 
    {
        // Remember you can use Processing's graphics methods here
        fill(255, 250, 240); //color white
        rect(25, 50, 150, 250); // (x location of upper left corner, y location of upper left corner, width, height)

        fill(0); //needed for text to appear, sets the color to fill shapes, takes in an int rgb value
        textAlign(LEFT, CENTER);
        textSize(12);
        text("Earthquake Key", 50, 75); //heading of key, takes (string, float x, and float y)

        fill(color(255, 0, 0)); //red
        ellipse(50, 125, 15, 15); //(x coordinate, y coordinate, width, height)   )
        fill(color(255, 255, 0)); //yellow 
        ellipse(50, 175, 10, 10);
        fill(color(0, 0, 255));
        ellipse(50, 225, 5, 5);

        fill(0, 0, 0);
        text("5.0+ Magnitude", 75, 125);
        text("4.0+ Magnitude", 75, 175); // same y coordinate but different x so it could appear right beside marker
        text("Below 4.0", 75, 225);
    }
}

【问题讨论】:

    标签: java google-maps-markers


    【解决方案1】:

    很高兴认识在COURSERA 注册同一课程的人。

    首先,这部分只是一个示例,向您展示如何将“幅度”提取为浮点数。

    // These print statements show you (1) all of the relevant properties 
    // in the features, and (2) how to get one property and use it
    
    if (earthquakes.size() > 0) {
        PointFeature f = earthquakes.get(0);
        System.out.println(f.getProperties());
        Object magObj = f.getProperty("magnitude");
        float mag = Float.parseFloat(magObj.toString());
    // PointFeatures also have a getLocation method
    }
    

    让我们在这个例子中检查对象的类型和思路。理解这部分对于完成作业非常重要。 “earthquakes.get()”这行意味着只从地震中获取第一个对象,即对象列表。

    • 地震:PointFeature 对象列表
    • f:地震中只有一个 PointFeature 对象
    • magObj:“幅度”值作为来自 f 的对象
    • mag:幅度的最终浮点值

    在这个问题中,你不需要定义一个新的变量。 我的伪代码是这样的。

    1. Use for~ loop
       for (PointFeature f : earthquakes) {  ...  }
    
    2. Create new instance of SimplePointMarker 
       Check API document of SimplePointMarker class.
       It needs a Location as a parameter and returns SimplePointMarker object.
       You can get the location parameter easily, 
       because SimplePointMarker implements Maker interface.
       Just use 'getLocation()' method. 
       You made a many markers (SimplePointMaker)!
    
       After making many makers, you can practice with some methods.
       Ex) marker.setColor(yellow), marker.setRadius(10) 
    
    3. What you have to do is only to add makers to map.
       Check API document again. (Map class)
       Map class has addMarker(Marker marker) method.
       You can use it out of for loop.
    
    4. Your addKey() method is Great!
    

    我希望这会对你有所帮助。

    【讨论】:

    • 我尝试循环遍历地震对象并使用 map.addMarker()。但是小程序显示黑屏并且没有任何反应。我尝试在不使用循环的情况下做同样的事情,我的意思是只用一个标记就可以了。但在循环中它没有!可能是什么原因!
    【解决方案2】:
     //add this in setup method    
     for(PointFeature ia: earthquakes) {
             SimplePointMarker to = createMarker(ia);//using given healper method
             to.setRadius(10.0f); 
    
             Object magObj = ia.getProperty("magnitude");
             float mag = Float.parseFloat(magObj.toString());
             if(mag >= 5.0f ){
    
                  to.setColor(red);
                  }
             else if(mag >= 4.0f ){
    
                  to.setColor(yellow);
                  }
             else{
    
                  to.setColor(blue);
                  }
    
              markers.add(to);//adding created simplepointmarkers to(to) arraylist
    
    
         }//end of for loop
    
       map.addMarkers(markers)//entire list of markers is  added to map
    }//end of setup()
    

    【讨论】:

      【解决方案3】:

      您应该能够在开始自定义之前放置默认标记(灰色)。

      您的createMarker 方法获取特定地震并返回带有位置的默认标记。因此,您应该能够将地震逐个传递到此方法中,以便为每次地震都有标记。 (这并不意味着您将更改 createMarker 方法。传递将由 setup 方法中的 for 循环完成。)

      我们将为特定地震做的事情将针对每次地震做,因此我们将从现在开始在for 循环中工作。创建默认标记后,您应该使用另一种方法在地图上绘制它。 (您可以通过LifeExpectancy.java 示例查看如何在地图上绘制标记。)

      当您设法绘制默认标记时,标记的颜色和大小不会有挑战性。

      【讨论】:

        【解决方案4】:

        我有一个完整的代码:

        List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this,earthquakesURL);
        
        for(PointFeature ft : earthquakes){
            markers.add(new SimplePointMarker(ft.getLocation(), ft.getProperties()));
        }
        
        map.addMarkers(markers);
        
        for(Marker mk1 : markers){
            if((float) mk1.getProperty("magnitude") > 5.0)
                mk1.setColor(red);
            else{
                if((float) mk1.getProperty("magnitude") > 4.0 && 
                   (float) mk1.getProperty("magnitude") < 5.0)
                    mk1.setColor(yellow);
                else
                    mk1.setColor(blue);
                }
            }
            map.addMarkers(markers);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-05-22
          • 2020-06-15
          • 2017-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多