我是Android的新手,使用谷歌地图Android API v2为我的应用程序,并有一个问题。我从服务器获得了一个位置(Latitude,Longgitude),我的工作在地图上设置了这个位置的标记,并自动保持更新标记的位置(例如:1更新/分钟)。 我可以从服务器获取位置并准确显示标记,但无法通过无限循环自动更新。我如何使用按钮自动更新标记和另一个停止? 在这里,我的代码从服务器获取数据(使用JSON)并设置标记:
iv_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
markers = new ArrayList<Marker>();
listTaxi = new ArrayList<Get1Comp>();
googleMap.clear();
count = 1;
txt_search = et_search.getText().toString();
find_url = "http://192.111.124.80:8001/Default.aspx?username="
+ Id + "&password=" + Pass + "&comp="+txt_search;
new AsynComp().execute();
getCurrentFocus().clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_search.getWindowToken(), 0);
}
});
public class AsynComp extends AsyncTask<Void, Void, Void> {
ProgressDialog taxiDialog;
@Override
protected Void doInBackground(Void... params) {
jsonComp = new JSONComp(find_url);
find_status = jsonComp.getJsonStatus(txt_search);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (find_status.equals("2013")) {
Toast.makeText(getBaseContext(), "No resutl",
Toast.LENGTH_SHORT).show();
} else if (find_status.equals("2012")) {
for (int i=0; i<count;i++){
listCom.add(new Get1Comp(jsonComp.getJsondata(i)));
SetMarkerComp(listComp.get(i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
private void SetMarkerComp (Get1Comp get1Comp){
LatLng target = new LatLng( Double.parseDouble(get1Comp.getLat()),
Double.parseDouble(get1Comp.getLng()));
String compStt = get1Comp.getStt();
String compNav = get1Comp.getNav();
if (compStt.equals("0")) compStt = "b";
else if (compStt.equals("1")) compStt = "y";
else if (compStt.equals("2")) compStt = "r";
else if (compStt.equals("3")) compStt = "w";
Marker marker = googleMap.addMarker(new MarkerOptions() .position(target)
.icon(BitmapDescriptorFactory.fromResource(getIconComp(compStt, compNav)))
.title(get1Comp.getNo()));
marker.showInfoWindow();
setCamPosition(target);
markers.add(marker);
}
答案 0 :(得分:1)
hiyou正在使用此代码
Handler locationHandler;
final static long REFRESH = 10 * 1000;
final static int SUBJECT = 0;
private GoogleMap mMap;
private Marker myMarker = null;
并添加到创建
locationHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == SUBJECT) {
updateMarker();
this.sendEmptyMessageDelayed(SUBJECT, REFRESH);
}
}
};
答案 1 :(得分:1)
您可以通过创建功能
来尝试public void callAsyncTask(){
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsyncTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
try{
// put your code here
new AsynComp().execute();
}catch (Exception e){
}
}
});
}
};
timer.schedule(doAsyncTask,0,10000);
}
并在onCreate()中调用此函数,您也可以保留新的AsynComp()。execute();目前的电流也是如此。这样用户可以强制刷新而不是自动,
答案 2 :(得分:0)
你可以使用for和loop。
for(int i = 0; i <= 1000; i++){
//code here
}