我是 Android 的新手,在我的应用程序中使用 Google Maps Android API v2 并且遇到了问题。我从服务器获得了一个位置(纬度,经度),我的工作是在地图上用这个位置设置一个标记,并保持自动更新标记的位置(例如: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);
}