2017-09-07 7 views
0

ポイントタイプのKMLファイルを読み込んで、Google Elevation APIを使用して標高を更新するアプリケーションを開発しました。あなたが見ることができるように、それはポイントの緯度と経度を受け取り、APIキーを付けて標高を取得します。私のKMLファイルが複数のポイントを持っているので、私は、ポイントの緯度と長い読みキーでそれを追加し、このようにGoogleの標高API.SomethingにURLを送信するためのThreadPoolを使用しました:アンドロイドスレッドプールの補完コールバック

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フラグを設定します。ここでは、スニペットは次のとおりです。

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フラグをチェックしますか?

答えて

0

以下のコードを変更してください。

  1. HeightTaskCallableインターフェイスを実装するように変更します。

  2. 呼び出し可能タスクのコレクションを用意し、使用して提出invokeAll()

    List<HeightTask > futureList = new ArrayList<HeightTask >(); 
    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);  
        futureList.add(taks);  
    } 
    executor.invokeAll(futureList); 
    

invokeAll:

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) 
         throws InterruptedException 

はその状態を保持先物のリストを返す、与えられたタスクを実行し、すべてが完了すると結果が返されます。 Future.isDone()は返されたリストの各要素について真です。完了したタスクは、通常どおりまたは例外をスローすることによって終了する可能性があることに注意してください。