[英]Why is this happening (strangeness with Processing and UnfoldingMaps)
作為coursera UCSD Java 課程的一部分,我們正在使用Processing 和Unfolding Maps 庫。
該課程為您提供了入門代碼,我正在嘗試對其進行擴展。 但是我遇到了一個問題。
在我的工作計算機上,我進行了大部分學習,我有一個非常好的應用程序啟動。 我決定通過將我的 github 存儲庫克隆到我的家用筆記本電腦,將它帶回家給我的妻子展示給她看。
沒有包含庫/JAR 文件,所以我直接從 Processing and Unfolding 的下載頁面下載了它們。 結果是一場噩夢。 一個又一個錯誤一個錯誤。 我假設 coursera/UCSD 使用了舊版本的庫,並且新版本不向后兼容。 看起來很糟糕,但無論如何。
接下來,我從coursera Sight 下載了所有JAR 文件。 結果好一點,但事情仍然很古怪。 也就是說,當我創建一個UnfoldingMap()
對象時,窗口內的位置參數和大小絕對沒有任何作用。
1.) 我使用 Processing 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();
}
最簡單的選擇是使用 Unfolding 仍然支持的過時版本 Processing,例如Unfolding 0.9.6 (for Processing 2.2.1) 。
由於您使用的是 eclipse,因此您實際上可以編譯一個可能對您有用的較新版本。 (我注意到去年有一些小的更新,但沒有發布)。 要通過 Eclipse 更新,您可以:
git
: git clone https://github.com/tillnagel/unfolding
:這樣您就可以隨時獲取最新更新並輕松重建, 否則下載 zip、解壓等)main
將編譯所有這些,包括 Processing 包裝器。如果您只想使用命令行:
PATH
環境變量中(例如檢查 Unix 上的echo $PATH
或 Windows 上的echo %PATH%
並確保列出了 ant 所在的文件夾,如果沒有添加它)ant unfolding_processing
僅構建處理包裝器或簡單地構建ant
(默認為構建所有內容的main
目標)我可以確認從 github 手動編譯最新版本適用於 Processing 3.4
我在這里上傳了處理包裝器:希望您可以將 .jar 文件拖到您的 eclipse 項目之上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.