【问题标题】:Android - Auto Update Marker Position Google map api v2Android - 自动更新标记位置谷歌地图 api v2
【发布时间】:2013-09-21 10:41:57
【问题描述】:

我是 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);
}

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你正在使用这个代码

    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);
                }
            }
        };
    

    【讨论】:

      【解决方案2】:

      你可以通过创建一个函数来试试这个

      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()中调用这个函数,也可以留下new AsynComp().execute();在当前的电流以及。这样用户就可以强制刷新而不是自动刷新,

      【讨论】:

        【解决方案3】:

        你可以使用 for 和循环。

        for(int i = 0; i <= 1000; i++){
         //code here 
        }
        

        【讨论】:

          猜你喜欢
          • 2012-11-29
          • 2016-10-12
          • 1970-01-01
          • 2013-08-09
          • 2013-03-09
          • 2015-02-04
          • 2015-10-18
          • 2012-12-07
          • 1970-01-01
          相关资源
          最近更新 更多