1

嗨,我需要在我尝试做的时候更新标记

ArrayList<LatLng> markerList = new ArrayList<LatLng>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_soc_map);

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    markerList.add(new LatLng(48.8742, 2.3470));
    markerList.add(new LatLng(0, 0));

Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask(){

    GetDataFromWeb GDFW = new GetDataFromWeb();

       public void run() {


         for (LatLng latLon : markerList) {
         mMap.addMarker(new MarkerOptions().position(latLon).title("Another marker"));
            }
           //GDFW.execute();                       
       }

    }, 12000, 12000);

} 

所以..我从数据库 Lat 和 Lng 中获取,但我只是用

markerList.add(new LatLng(48.8742, 2.3470));
markerList.add(new LatLng(0, 0));

所以..我需要在 12 秒后获取 Lat 和 Lag 并更新地图标记

我还没有找到任何答案,所以也许你可以帮助我:)

4

1 回答 1

0

嘿,我刚刚遇到了同样的问题并解决了。

public class Livebus extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.googlemaps);

        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mTask = (GetDataFromWeb) new GetDataFromWeb().execute(); //This is an AsyncTaskClass
    }


class GetDataFromWeb extends AsyncTask<String, String, String> {

       protected String doInBackground(String... param) {

           while(true)
           {
               //HERE You should collect your data from the Web
               latitude=...
               longitude=...

               //You have to update the markers in the Main thread which is also called UI thread.
               //AsyncTask classes create theit own thread so you need to run the marker 
               //update on the UI thread. Which you can see below now.

               runOnUiThread(new Runnable() { 
                   public void run() {
                       mMap.addMarker(new MarkerOptions()
                       .position(new LatLng(latitude, longitude))
                       .title("Hello"));
                   }
               });
           }
     }
}

这是我在这里的第一个答案,希望对您有所帮助:) 如果有什么问题,请与我联系,我会让它正常工作。

AsyncTasks的详细信息:http: //developer.android.com/reference/android/os/AsyncTask.html 线程的详细信息:http: //developer.android.com/guide/components/processes-and-threads.html

读到我今天也吸取了教训。

于 2013-08-04T17:02:57.767 回答