P72B / PolylineAnimator

Android google maps polyline animator library
MIT License
24 stars 5 forks source link

Dynamically update more waypoints - crashed #4

Closed vijayakumarmariyappan closed 3 years ago

vijayakumarmariyappan commented 3 years ago

Hi Dude @P72B , The library works fine if I give fixed list of points. but if i update the waypoints while user moves. it crashes.

I want to update the latitude and longitue points when the user moves in map.

    if (animatedPolyline ==  null){
        animatedPolyline = AnimatedPolyline(
                this.maps!!,
                wayPoints,
                duration = 1250,
                interpolator = DecelerateInterpolator(),
                animatorListenerAdapter = getListener()
        )
        animatedPolyline?.startWithDelay(1000)
 }

then everytime user moves in map, then I add the

wayPoints.add(LatLng(location.latitude!!,location.longitude!!))

it always crashes with the below log.. could u please help me. Thanks for your help :)

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) at de.p72b.maps.animation.util.CalculationHelper.polylineUntilSection(CalculationHelper.kt:23) at de.p72b.maps.animation.AnimatedPolyline.onAnimationUpdate(AnimatedPolyline.kt:82) at android.animation.ValueAnimator.animateValue(ValueAnimator.java:1547) at android.animation.ValueAnimator.setCurrentFraction(ValueAnimator.java:674) at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:637) at android.animation.ValueAnimator.start(ValueAnimator.java:1069) at android.animation.ValueAnimator.start(ValueAnimator.java:1088) at de.p72b.maps.animation.AnimatedPolyline.start(AnimatedPolyline.kt:54) at de.p72b.maps.animation.AnimatedPolyline$startWithDelay$1.run(AnimatedPolyline.kt:59) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

P72B commented 3 years ago

Hi vijayakumarmariyappan,

please make sure to use animatedPolyline.replacePoints(newWayPointList) with the list of updated waypoints.

Thank you for the feedback. I will consider to update the library to be able to directly modify the given list of wayPoints inside the AnimatedPolyline constructor. That would make the replacePoints method obsolute.

Let me know if replacePoints works out for you.

vijayakumarmariyappan commented 3 years ago

@P72B Thank you so much for your reply. I will check and update you :)

vijayakumarmariyappan commented 3 years ago

@P72B

I tried the below, but still I'm getting the same crash again and again. May be your eyes could see the issue in my code. Thanks for your help :)

// This method will get called whenever the user move from one place to another.

      override fun setLocation(location: Location) {

            latitude = location.latitude!!
            longitude = location.longitude!!

            wayPoints.add(LatLng(location.latitude!!,location.longitude!!))

            **// if it has 4 points then add the animated polyline**
            if (animatedPolyline ==  null && wayPoints.count()==4){//4
                animatedPolyline = AnimatedPolyline(
                        this.maps!!,
                        wayPoints.toList(),
                        duration = 1250,
                        interpolator = DecelerateInterpolator(),
                        animatorListenerAdapter = getListener()
                )
                animatedPolyline?.start()

            } else {
                **// from 5 points thereafter update the points**
                if (wayPoints.count()>4){//5,6,...
                    animatedPolyline?.replacePoints(wayPoints.toList())
                } else {//1,2,3
                    **// if it is less than 4 points then do nothing**
                }
            }

    }
P72B commented 3 years ago

In case the fix is not working for you feel free to reopen the issue.

Here I have an example code based on your snipped I used for fixing the issue:

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var map: GoogleMap
    private lateinit var animatedPolyline: AnimatedPolyline

    private val wayPoints = mutableListOf<LatLng>()
    private var lastDegree = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)

        startDemo()
    }

    override fun onMapReady(googleMap: GoogleMap) {
        map = googleMap

        animatedPolyline = AnimatedPolyline(
            map,
            wayPoints,
            duration = 1250,
            interpolator = DecelerateInterpolator()
        )
    }

    private fun setLocation(location: Location) {
        wayPoints.add(LatLng(location.latitude,location.longitude))

        if (wayPoints.size < 5 || !this::animatedPolyline.isInitialized) {
            return
        }

        animatedPolyline.start()
    }

    private fun startDemo() {
        object : CountDownTimer(80000, 2000) {
            override fun onTick(millisUntilFinished: Long) {
                setLocation(calculateNextLocation())
            }

            override fun onFinish() {
                // nothing
            }
        }.start()
    }

    private fun calculateNextLocation(): Location {
        lastDegree += 2
        return Location("").apply {
            latitude = lastDegree
            longitude = 0.0
        }
    }
}
P72B commented 3 years ago

Please make use of the updated lib version 0.1.4 containing the fix. In case it's not solving the issue feel free to reopen the issue. Thank you for the feedback.

P72B commented 3 years ago

FYI: replacePoints function is not required anymore to be called to update the list of waypoints in your example. Please check the code snipped I provided above.

vijayakumarmariyappan commented 3 years ago

@P72B so kind of you dude. I will check and update :)

vijayakumarmariyappan commented 3 years ago

@P72B Thank you much :+1: . it works now :100: Thanks for your valuable time :)