作为 coursera UCSD Java 课程的一部分,我们正在使用处理和展开地图库。
该课程为您提供了我正在尝试扩展的入门代码。但是我遇到了一个问题。
在我的工作计算机上,我大部分时间都在其中学习,我启动了一个非常好的应用程序。我决定把它带回家给我妻子看,把我的 github repo 克隆到我的家用笔记本电脑上。
没有包含任何库/JAR 文件,所以我直接从 Processing and Unfolding 的下载页面下载了它们。结果是一场噩梦。一个又一个的错误,一个又一个的错误。我假设 coursera/UCSD 使用了旧版本的库,而新版本不向后兼容。看起来很糟糕,但无论如何。
接下来,我从 coursera 视线中下载了所有的 JAR 文件。结果稍微好一点,但事情仍然很古怪。也就是说,当我创建一个UnfoldingMap()
对象时,窗口内的位置参数和大小绝对没有任何作用。
1.)我使用处理size()
方法来放大或缩小包含窗口,它会放大或缩小地图对象。它完全忽略了UnfoldingMap()
.
2.) 地图对象总是被推到窗口的左下角,不管它的位置参数。
3.) 我选择的灰色background()
确实显示在地图周围,但背景本身被一个大的黑色区域包围,它也随size()
参数缩放。
这是一些代码:
package module3;
//Java utilities libraries
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
//Processing library
import processing.core.PApplet;
//Unfolding libraries
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.marker.Marker;
import de.fhpotsdam.unfolding.data.PointFeature;
import de.fhpotsdam.unfolding.marker.SimplePointMarker;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.providers.MBTilesMapProvider;
import de.fhpotsdam.unfolding.utils.MapUtils;
//Parsing library
import parsing.ParseFeed;
public class EarthquakeCityMap extends PApplet {
// 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 earthquakesURLweek = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.atom";
private String earthquakesURLday = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom";
public void setup() {
size(400, 400, "processing.opengl.PGraphics3D");
background(99);
map = new UnfoldingMap(this, 50, 50, 1100, 700, new Google.GoogleMapProvider());
map.zoomToLevel(2);
MapUtils.createDefaultEventDispatcher(this, map);
//List of markers to be added to map
List<Marker> markers = new ArrayList<Marker>();
//Use parser to collect properties for each earthquake
//PointFeatures have a getLocation method
List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this, earthquakesURLweek);
// for each earthqauke feature, create a custom marker and add it
// to the list.
for (PointFeature quake : earthquakes){
Marker quakeMark = createMarker(quake);
markers.add(quakeMark);
Map<String, Object> properties = quakeMark.getProperties();
}
// Add the markers to the map so that they are displayed
map.addMarkers(markers);
}
/**
* A helper method to style markers based on features (magnitude, depth,
* etc) of an earthquake.
* @param feature A PointFeature object representing a single earthquake.
* @return
*/
private SimplePointMarker createMarker(PointFeature feature){
// Create a new SimplePointMarker at the location given by the PointFeature
SimplePointMarker marker = new SimplePointMarker(feature.getLocation(), feature.getProperties());
Object magObj = feature.getProperty("magnitude");
Object ageObj = marker.getProperty("days ellapsed");
float mag = Float.parseFloat(magObj.toString());
int age = (int) ageObj;
//Set processing color and alpha data, for setting marker colors
//below.
int alpha = 255 - (age * 255 / 7);
int yellow = color(255, 255, 0, alpha);
int red = color(255, 0, 0, alpha);
int green = color(0, 255, 0, alpha);
// Style markers based on earthquake magnitude
if (mag < THRESHOLD_LIGHT){
marker.setColor(green);
}
if (mag >= THRESHOLD_LIGHT && mag < THRESHOLD_MODERATE){
marker.setColor(yellow);
}
if (mag >= THRESHOLD_MODERATE){
marker.setColor(red);
}
//set radius of marker based on quake magnitude
float radius = (float) (mag * 3.5);
marker.setStrokeColor(color(50,15));
marker.setRadius(radius);
return marker;
}
public void draw() {
map.draw();
addKey();
}