我需要添加代码以在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);
}
}
答案 0 :(得分: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
}
让我们在这个例子中检查对象的类型和思想的流程。理解这部分以完成作业非常重要。 “earthquakes.get()”这一行意味着只从地震中获取第一个对象,即对象列表。
在此问题中,您无需定义新变量。 我的伪代码是这样的。
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!
我希望这会对你有所帮助。
答案 1 :(得分: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()
答案 2 :(得分:0)
在开始自定义之前,您应该能够设置默认标记(灰色)。
您的createMarker
方法获取特定地震并返回带有位置的默认标记。所以,你应该能够将地震一个接一个地传递给这个方法,以便为每次地震提供标记。 (这并不意味着您将更改createMarker
方法。传递将由安装方法中的for
循环完成。)
我们将为每次地震做一个特定地震的事情,所以我们将从现在开始for
循环。创建默认标记后,应使用其他方法在地图上绘制它。 (您可以通过LifeExpectancy.java
示例检查如何将标记绘制到地图上。)
当您设法绘制默认标记时,标记的着色和大小调整不会具有挑战性。
答案 3 :(得分:-1)
我有一个完整的代码:
error: no matching function for call to ‘execute(<brace-enclosed initializer list>)