简体   繁体   English

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

[英]Adding markers to a unfolding map in Java

I need to add code to display a marker at the location of each earthquake in the RSS feed and make the radius of each marker 10 by modifying the methods (and adding new helper methods as necessary). 我需要添加代码以在RSS提要中的每个地震的位置显示一个标记,并通过修改方法(并根据需要添加新的辅助方法)来使每个标记的半径为10。 Then I need to add code to style each marker according to the magnitude of its earthquake. 然后,我需要添加代码来根据标记的震级对每个标记进行样式设置。

I know that I should create a SimplePointMarker object for each PointFeature in the list features, inside the setUp method for making markers appear on the screen, so it'll probably involve for each loops, but other then that I'm not sure how to code this. 我知道我应该为列表功能中的每个PointFeature创建一个SimplePointMarker对象,在setUp方法内部以使标记出现在屏幕上,因此它可能涉及每个循环,但是除此之外,我不确定如何对此进行编码。

Unfolding marker package explanation... http://unfoldingmaps.org/javadoc/ 展开标记包说明... 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);
    }
}

It's very glad to meet a person who enrolled the same course in COURSERA. 很高兴认识一个在COURSERA报读相同课程的人。

First, this part is just a example to show you how to extract "magnitude" as a float. 首先,这部分只是一个示例,向您展示如何提取“量”作为浮点数。

// 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
}

Let's check types of object and flow of thought in this example. 在此示例中,让我们检查对象的类型和思维流程。 It's very important to understand this part to complete assignment. 了解这部分以完成分配是非常重要的。 The line "earthquakes.get()" means to get only the first object from the earthquakes, list of objects. “ earthquakes.get()”行意味着仅从地震中获取第一个对象,即对象列表。

  • earthquake: List of PointFeature Objects 地震:PointFeature对象列表
  • f: just ONE PointFeature object from earthquakes f:地震中只有一个PointFeature对象
  • magObj: "magnitude" value as a object from f magObj:“幅值”值作为f中的一个对象
  • mag: final float value of magnitude mag:大小的最终浮点值

In this problem, you don't need to define a new variable. 在此问题中,您无需定义新变量。 My pseudocode is like this. 我的伪代码是这样的。

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!

I hope this will help you. 我希望这能帮到您。

 //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()

You should be able to put the default markers (grey) before you start to customize them. 在开始自定义默认标记之前,您应该能够放置默认标记(灰色)。

Your createMarker method gets a specific earthquake and returns a default marker with location. 您的createMarker方法会发生特定的地震,并返回带有位置的默认标记。 So, you should be able to pass the earthquakes into this method one by one to have markers for every earthquake. 因此,您应该能够将地震一步一步地传递到此方法中,以便为每次地震提供标记。 (That does not mean you will change the createMarker method. Passing will be done by a for loop inside the setup method.) (这并不意味着您将更改createMarker方法。传递将通过setup方法内的for循环进行。)

A thing we will do for a specific earthquake will be done for every earthquake, so we will work in the for loop from now on. 我们将为每次地震完成针对特定地震的操作,因此从现在开始我们将在for循环中工作。 Once you created a default marker, you should draw it on the map with another method. 创建默认标记后,您应该使用另一种方法在地图上绘制它。 (You can check on how to draw the markers onto the map via the LifeExpectancy.java example.) (您可以通过LifeExpectancy.java示例检查如何在地图上绘制标记。)

The coloring and sizing of the markers won't be challenging when you manage to draw the default ones. 当您设法绘制默认标记时,标记的颜色和大小将不会受到挑战。

I have a complete code for this: 我有一个完整的代码:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM