我是Java和Eclipse的新手,在弄清楚如何执行createMarker指令时遇到了麻烦。下面的代码运行正常,但它的编写方式与注释中的TODO (step 3)所说的不同。
例如,当我试图在循环中调用createMarker方法时,我这样写它:
markers.createMarker(new SimplePointMarker(earthquake.getLocation(), earthquake.getProperties())) 但是我得到了错误The method createMarker is undefined for the type List Marker。
谁能告诉我在循环中调用createMarker的正确方法?我已经尝试了课堂论坛和之前的Stack Overflow问题,但我还没有找到答案。
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;
}发布于 2018-01-05 04:37:38
我假设在方法setup()的末尾有一个缺失的},在
map.addMarkers(markers);然后,您应该能够在已经存在的for-loop中调用createMarker。
请记住,createMarker接受PointFeature ( for中使用的earthquake变量)作为输入。createMarker本身返回一个可以添加到标记列表中的SimplePointMarker。所以它可能看起来像这样:
for (PointFeature earthquake : earthquakes) {
markers.add(createMarker(earthquake));
}https://stackoverflow.com/questions/48102988
复制相似问题