1

I have developed an android application which shows the near by vehicles for which latlng will be saved to server on locationchanged which will be the jsonarray result from which i will be able to add the markers on mapbox map through animation i am able to move the single marker to the next updated latlng values. I am not able to move all the markers displayed on the navigation view of the map simultaneously as they are updated. Marker moves one by one to the updated location

HashMap<Integer, Marker> markerhash = new HashMap<Integer, Marker>();
if (json.getString("status").equalsIgnoreCase("success")) {
  Alllocations = json.getJSONArray("locations");
  for (int i = 0; i < Alllocations.length(); i++) {
    JSONObject LocInfo = Alllocations.getJSONObject(i);
    int id = LocInfo.getInt("user_id");
    if (markerhash.containsKey(id)) {
      marker = markerhash.get(id);
      marker.setIcon(icon);
      ValueAnimator markerAnimator = ValueAnimator.ofObject(new LatLngEvaluator(), (Object[]) new LatLng[] {marker.getPosition(), new LatLng(LocInfo.getDouble("latitude"), LocInfo.getDouble("longitude"))});

      markerAnimator.setDuration(2000);
      markerAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
      markerAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
          if (marker != null) {
            marker.setPosition((LatLng) animation.getAnimatedValue());
          }
        }
      });
      markerAnimator.start();

    } else {
      marker = navigationView.getMapboxMap().addMarker(new MarkerOptions()
        .position(new LatLng(LocInfo.getDouble("latitude"), LocInfo.getDouble("longitude")))
        .snippet(String.valueOf(LocInfo.getInt("user_id")))
        .icon(icon)
      );
      markerhash.put(id, marker);

    }
  }
}
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
beetle
  • 11
  • 4

1 Answers1

0

You need to post code in order to get proper support!

That said have you considered simply removing all markers and drawing them again at the same time for each iteration?

Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • since the json array iterates through for loop i am not able to animate all the markers continuously it is being updated one by one. Sometimes the marker flickers since other json array result come fast by the time the marker completes it animation. may i know without using loop can we add all marker at a time and update them all together. – beetle Aug 15 '18 at 10:35
  • @beetle I'd simply do it without the animation. Simply put all the markers at their new position. How often do you update the position? – Warpzit Aug 15 '18 at 10:49
  • onlocaton changed the json array results will be called to sync in real time and i need to make marker move as they travel in their vehicle. so i need to know without using loop is there any method to add all markers at a time and update all at a time – beetle Aug 15 '18 at 11:49
  • @beetle If you insist on doing it that way you could try to wrap each loop in a thread. But I still think the best way to go is to skip the animation and simply update the new position of all the markers. – Warpzit Aug 15 '18 at 12:00
  • why is animation a problem here? okay can you help me with wrapping each loop in a thread for the above code – beetle Aug 15 '18 at 12:32
  • @beetle After reading a little I think you need to use https://developer.android.com/reference/android/animation/AnimatorSet . But the above code you've posted should work too as far as I can see. – Warpzit Aug 16 '18 at 11:40
  • above code is working but when there are more than two markers appears as soon as it completes the for loop other markers will since onLocation changed is called every 3sec the json array results will appear so marker will not have time to completes its animation because it iterates in loop so i am asking alternative method without using iteration method – beetle Aug 17 '18 at 06:45
  • @beetle Well as I see it you have 2 options: 1. Don't use animation and simply place the markers at their new positions. 2. Let the animations finish before starting new animations. – Warpzit Aug 17 '18 at 07:38