添加新

时间:2018-05-14 15:06:48

标签: java android google-maps google-maps-markers

我目前正在开发一款向学生展示公交车位置的应用程序! 问题是当我从地图中删除带有marker.remove();map.clear();的标记并检索新位置并在地图上显示时,旧的仍会显示。
这是我的代码:

@Override
  public void onMapReady(final GoogleMap googleMap) {
    map = googleMap;
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(false);

    new Timer().scheduleAtFixedRate(new TimerTask() {
      @Override
      public void run() {
        Webi.with(getContext())
          .from("http://31.25.91.60:8888/GetResult.php")
          .onResponse(new OnResponse() {
            @Override
            public void Response(String content, String s1) {
              try {
                JSONArray arr = new JSONArray(content);
                if(arr.length() > 0) {
                  for (int i = 0; i < arr.length(); i++) {
                    JSONObject jobj = arr.getJSONObject(i);

                    name.add(jobj.getString("driverid"));
                    lat.add(jobj.getString("lat"));
                    latlong.add(jobj.getString("longlat"));
                    time.add(jobj.getString("time"));
                  }

                  for(int index = 0; index < name.size(); index++) {
                    final int finalIndex = index;
                    getActivity().runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                        try {
                          LatLng point = new LatLng(Double.parseDouble(lat.get(finalIndex)),Double.parseDouble(latlong.get(finalIndex)));
                          MarkerOptions markerOptions = new MarkerOptions()
                            .position(point)
                            .title(name.get(finalIndex))
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_pin));
                          marker = map.addMarker(markerOptions);
                          markers.add(marker);
                        } catch (Exception e) {
                          e.printStackTrace();
                        }
                      }
                    });
                  }
                } else {
                  Toast.makeText(getActivity(), "چیزی پیدا نشد !", Toast.LENGTH_SHORT).show();
                  map.clear();
                }
              } catch (Exception e) {
                map.clear();
                e.printStackTrace();
              }
            }
          }).connect();
      }
    }, 0, 10000);
    LatLng university = new LatLng(36.2497511,58.8287331);
    CameraPosition cameraPosition = new CameraPosition.Builder()
      .target(university)
      .zoom(17)
      .bearing(90)
      .tilt(40)
      .build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  }

  private Runnable runnable = new Runnable() {
    @Override
    public void run() {
      try {
        for(int index = 0; index < markers.size(); index++) {
          marker = markers.get(index);
          marker.remove();
          map.clear();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      handler.postDelayed(this, 9000);
    }
  };

每10秒从服务器提取一次,每9秒标记必须删除

1 个答案:

答案 0 :(得分:0)

尝试将您的map.clear()和整个for循环移动到UI线程Runnable中。我认为你总是希望在添加新标记之前清除地图。否则,您将在同步这两个操作时遇到问题。例如,在2次运行后,标记将在18秒时被移除,然后直到20秒才被添加。

此外,我没有看到您清除数据列表的位置,是否在添加新响应数据之前执行此操作?

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        map.clear();
        for(int index = 0; index < name.size(); index++) {
            try {
                LatLng point = new LatLng(Double.parseDouble(lat.get(index)), Double.parseDouble(latlong.get(index)));
                MarkerOptions markerOptions = new MarkerOptions()
                        .position(point)
                        .title(name.get(index))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_pin));
                marker = map.addMarker(markerOptions);
                markers.add(marker);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
});