如何使用ScheduledExecutorService每30秒在地图上添加标记

时间:2014-03-27 08:26:32

标签: android google-maps

我是Android编程的新手。我想在谷歌地图上绘制标记。对于这个例子,我每隔30秒就使用硬编码的lat,lng和plot标记。我在这个应用程序中使用了ScheduledExecutorService。但是我的地图上看不到标记。在logcat中,日志可见。有人可以帮助我或指出正确的解决方案。以下是我的代码段。

    scheduleTaskExecutor= Executors.newScheduledThreadPool(5);

    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {

          Log.d("scheduleTaskExecutor log : ","run every 10 seconds");

        mMap.addMarker(new MarkerOptions()
        .position(defaultLatLng)
        .title("T69")
        .snippet("Your're here"));


      }
    }, 0, 30, TimeUnit.SECONDS);

TQ

1 个答案:

答案 0 :(得分:0)

好的..我得到了解决方案。刚刚使用了runOnUiThread。示例如下:

    scheduleTaskExecutor= Executors.newScheduledThreadPool(5);

    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {
          Log.d("RUN RUN RUN ","run every 10 seconds");
          runOnUiThread(new Runnable() {
            public void run() {
              // update your UI component here.
           try {
                mMap.addMarker(new MarkerOptions()
                .position(defaultLatLng)
                .title("T69));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("MAP" ,e.getMessage());
            }
            }
          });
        }
    }, 0, 10, TimeUnit.SECONDS);
相关问题