public class TestActivity extends Activity implements OnMarkerClickListener{
  static  LatLng HAMBURG = new LatLng(53.558, 9.927);
  static  LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;
  Marker hamburg, kiel;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
    hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
    kiel = map.addMarker(new MarkerOptions().position(KIEL).title("Kiel"));

    Polyline line = map.addPolyline(new PolylineOptions().add(HAMBURG,KIEL).width(5).color(Color.RED));

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);


     AsyncTaskRunner runner = new AsyncTaskRunner();
     runner.execute();
  }



  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }


  private class AsyncTaskRunner extends AsyncTask<String, String, String> {

      String result;ProgressDialog pdia;

      @Override
      protected String doInBackground(String... params) {
       publishProgress("Loading..."); // Calls onProgressUpdate()
        TestActivity.HAMBURG = new LatLng(3.190393, 101.651317);
        TestActivity.KIEL = new LatLng(3.148443,101.577099);

       return result;
      }


      @Override
      protected void onPostExecute(String result) {

       pdia.dismiss();
      }


      @Override
      protected void onPreExecute() {

          super.onPreExecute();
          pdia = ProgressDialog.show(TestActivity.this, "Please wait", "Loading data from database.", true, false);
      }


      @Override
      protected void onProgressUpdate(String... text) {

      }
     }
}


我需要执行的是将标记(HAMBURG和KIEL)更改为异步任务中设置的位置。同样,在列表视图中,我使用notifydatachanged(),在这种情况下该怎么办?

请说明一下,并先谢谢您。

最佳答案

没有像notifyDataChanged中的ListView方法,您需要手动更改标记的位置。

首先,创建kielline类级字段,例如hamburg。然后,在您的AsyncTaskonPostExecute方法中,添加:

hamburg.setPosition(HAMBURG);
kiel.setPosition(KIEL);
line.setPoints(Arrays.asList(new LatLng[] { HAMBURG, KIEL }));

10-08 12:32