Android谷歌地图v2自动更新

时间:2013-04-28 19:27:35

标签: google-maps maps android-maps-v2

嗨我需要在尝试时更新标记

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并更新地图标记

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

1 个答案:

答案 0 :(得分: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

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