我已經使用GoogleMap創建了一個應用程序。 我想做一些數學計算,並在一直運行的線程中更新GoogleMap的標記。如何將UI元素的信息返回到線程
我的線程需要知道當前googleMap的攝像頭縮放以及LatLngBounds來更新標記。
如果我這樣做,它完美的作品:
class updateMap implements Runnable{
private double currentZoom;
private LatLngBounds screenRegion;
@Override
public void run(){
while(!threadStop){//threadStop is a boolean to stop threads when onDestroy() is called
screenRegion = googleMap.getProjection().getVisibleRegion().latLngBounds;//fetches the screen's visible region
currentZoom = googleMap.getCameraPosition().zoom;
if(googleMap != null){//if my googleMap is fine
if(currentZoom >= 13){ //if the Camera's zoom is greater or equal than 13
//do some maths...
//updates the markers on the GoogleMap
}
}
}
}
}
,但我知道這是不推薦使用,所以我已經把代碼的幾行,與UI交互直接內螺紋與UI線程交互線程內的方法runOnUiThread()如下,但它不起作用。我的應用程序凍結,沒有任何反應,ARN甚至不顯示!
class updateMap implements Runnable{
private double currentZoom;
private LatLngBounds screenRegion;
boolean map = false;
@Override
public void run(){
while(!threadStop){//threadStop is a boolean to stop threads when onDestroy() is called
runOnUiThread(new Runnable() {//Here I execute this piece of code on the UI thread to get the variables
@Override
public void run() {
if(googleMap != null){
map = true;
screenRegion = googleMap.getProjection().getVisibleRegion().latLngBounds;//fetches the screen's visible region
currentZoom = googleMap.getCameraPosition().zoom;
}
}
});
if(map){//if my googleMap is fine
if(currentZoom >= 13){ //if the Camera's zoom is greater or equal than 13
//do some maths...
runOnUiThread(new Runnable(){
@Override
public void run(){
//updates the markers on the GoogleMap
}
});
}
}
}
}
}
如果有人有什麼想法?也許我不尊重語法,也許我不應該兩次使用runOnUiThread()方法...
在此先感謝。
數學是你在做耗時? – cliffroot 2014-10-22 12:09:03
不,只有幾個循環來檢查我想添加到地圖上的項目是否在地圖的可見區域內。 – gotgot1995 2014-10-22 14:04:03