如何从JSON数据中删除地图上的标记?

时间:2018-05-22 14:50:05

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

我有这样的json数据:

[
 {
  "id": 1,
  "name": "name",
  "login": "login",
  "lat": "43.854891",
  "lng": "73.536660"
 },
 {
  "id": 2,
  "name": "name",
  "login": "login",
  "lat": "53.954891",
  "lng": "73.536660"
 }
]

我从json获取所有数据并显示它们,但是当位置发生变化时,我无法在不删除旧位置的情况下显示新数据。还有一个当前位置,显示和删除旧位置。这是我的代码:

    private static final String REQUEST_URL = "http://192.168.1.103:8080/";

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

    checkPermission();
    buildGoogleApiClient();
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    LocationSearchTask locationSearchTask = new LocationSearchTask();
    locationSearchTask.execute(REQUEST_URL);

}


@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, location.toString());
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    if (currentMarker != null) {
        currentMarker.remove();
    }
    displayLocation(latLng, 15f, location);
    updateOtherLocations();
}  
public void updateOtherLocations() {
    LocationSearchTask locationSearchTask = new LocationSearchTask();

    locationSearchTask.execute(REQUEST_URL);
}

class LocationSearchTask extends AsyncTask<String, Void, List<User>> {

  @Override
    protected List<User> doInBackground(String... urls) {
        try {
            if (urls.length < 1 || urls[0] == null) {
                return null;
            }
            List<User> result = QueryData.fetchUsersData(urls[0]);
            return result;
        } catch (Exception e) {
            Toast.makeText(MapsActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    @Override
    protected void onPostExecute(List<User> users) {
        try {

            for (User user : users) {
                LatLng position = new LatLng(user.getLat(), user.getLng());
                // add marker
                otherMarkers = mMap.addMarker(new MarkerOptions()
                        .position(position)
                        .title(user.getFirstName())
                        .icon(bitmapDescriptorFromVector(getApplicationContext(), R.mipmap.ic_navigation))
                        .snippet(user.getEmail()));
            }

        } catch (Exception e) {
            Toast.makeText(MapsActivity.this, "Sorry other locations are unavailable! Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

我如何删除** otherLocations **并显示新位置? 我知道这是一个糟糕的代码,如果你能告诉我还有什么值得改变的话。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用mMap.clear()从地图中删除所有标记。 如果您想更新标记位置(如果是当前标记),请保留以前添加的标记,然后更改为marker.setPosition(new LatLng(50,5));

答案 1 :(得分:0)

标记上有一个现有字段可以使它们可见或不可见 请参见标记文档中的可见