在Java中将标记添加到展开的地图中

1 java google-maps-markers

我需要添加代码以在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

很高兴认识一个在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
}
Run Code Online (Sandbox Code Playgroud)

让我们在这个例子中检查对象的类型和思想的流程.理解这部分以完成作业非常重要."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!
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您.