【发布时间】:2018-02-15 18:54:03
【问题描述】:
我开发了一个应用程序来读取点类型的 KML 文件,然后使用 Google Elevation API 更新它们的高程。如您所见,它接收点的纬度和经度,并将其附加一个 API 密钥以检索海拔。因为我的 KML 文件有多个点,所以我使用 ThreadPool 来读取点的纬度和经度,将其附加到密钥,然后将 URL 发送到 Google Elevation API。像这样:
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(CORE_NUMBERS + 1);
String providerURL = provider.getServiceURL();
String providerKey = provider.getServiceAPIkey();
for (PointFeature p: points) {
String coordinate = p.getLatitude() + "," + p.getLongitude(); // get latitude and longitude of the feature
String url = providerURL + "locations=" + coordinate + providerKey; // creating the url of web service which contains coordinate
HeightTask task = new HeightTask(url, p); // task of each points
executor.execute(task);
}
heightTask 类是我从 API 解析 JSON 结果并获取高程并设置 heithUpdate 标志的地方。这是sn-p:
public class HeightTask implements Runnable {
private String url;
private Feature feature;
public HeightTask(String url, Feature f) {
this.feature = f;
this.url = url;
}
@Override
public void run() {
if (feature instanceof PointFeature) {
float height = GoogleAPIJsonParser.parsePoint(HttpManager.getData(url));
if (height != Float.NaN){
feature.updateHeight(height);
feature.setHeightUpdated(true);
Log.d("elevationPoint",height+"");
}
}
}
}
我需要一个回调来了解图层中所有点的高程是否已更新。 threadPool 中是否有任何模式,或者只是遍历所有点并检查 hieghtUpdate 标志?
【问题讨论】:
标签: android multithreading callback threadpool