1

I'm trying to simulate the real-time movement of a vehicle along a pre-determined path (as a list of coordinates) on ipyleaflet (running on a Jupyter Notebook). What I've tried is to instantiate a marker, and simply use a for loop to update the marker's position attribute to the next position in the path list and then re-display the updated position.

This kinda works as in it does show a map with the marker moving "in real-time", but with each iteration of the loop another copy of the same map is added to the notebook.

How can I avoid this so that the marker is updated in-place without any additional maps displayed?

Here's the code:

from ipyleaflet import Map, Marker, AntPath

# Center the map at the average X-Y points of the path:
center = (33.55423099111111, -7.624501421111111)

# Create a map object:
m = Map(center=center, zoom=13)

# Route to be taken: (normally this path is computed & stored in a DB but I copy-pasted it for simpler
# reproducibility)
path = [ [33.5411488, -7.5794811],
         [33.5412402, -7.5795595],
         [33.5417331, -7.57996],
         [33.5418974, -7.5800929],
         [33.5432614, -7.5812225],
         [33.5433477, -7.5813008],
         [33.5444994, -7.5822671],
         [33.5445967, -7.5823491],
         [33.544988, -7.5827073],
         [33.5476859, -7.5849085],
         [33.5477722, -7.5849964],
         [33.5486292, -7.5859519],
         [33.5487275, -7.5857875],
         [33.5491853, -7.5864057],
         [33.5499662, -7.5874609],
         [33.5498439, -7.587587],
         [33.5506432, -7.5884567],
         [33.5511799, -7.5889557],
         [33.5515046, -7.5892899],
         [33.5522733, -7.5900054],
         [33.5527107, -7.5904027],
         [33.553079, -7.5907443],
         [33.5533807, -7.5910218],
         [33.5543522, -7.5919123],
         [33.5544572, -7.5920105],
         [33.5547075, -7.5922415],
         [33.555303, -7.5927779],
         [33.556003, -7.5934134],
         [33.5561335, -7.5934934],
         [33.5573642, -7.5938461],
         [33.5574403, -7.5937775],
         [33.5575852, -7.5937772],
         [33.5577144, -7.5939521],
         [33.5587309, -7.5941295],
         [33.5604774, -7.5944197],
         [33.5623692, -7.5947609],
         [33.5628041, -7.5948519],
         [33.5627912, -7.5950528],
         [33.5623265, -7.5949738],
         [33.561216, -7.5947657],
         [33.5606722, -7.5946638],
         [33.5602063, -7.5945765],
         [33.5599793, -7.5945461],
         [33.5587228, -7.5983465],
         [33.5575395, -7.603906],
         [33.5564333, -7.6094216],
         [33.5540941, -7.6207419],
         [33.5529865, -7.6261197],
         [33.5523421, -7.6292593],
         [33.5503488, -7.63882],
         [33.5483646, -7.6451374],
         [33.5475533, -7.6462177],
         [33.5452278, -7.6508047],
         [33.5422984, -7.6558144],
         [33.5405838, -7.6587846],
         [33.5399228, -7.6598597],
         [33.5390237, -7.6615873],
         [33.5390865, -7.6627075],
         [33.5408139, -7.6629631],
         [33.5417034, -7.6628033],
         [33.5433927, -7.6634072],
         [33.5434618, -7.6635324],
         [33.544123, -7.6650199],
         [33.5445102, -7.6658912],
         [33.5448511, -7.666659],
         [33.5451912, -7.6674301],
         [33.5454642, -7.6680375],
         [33.5458589, -7.6689281],
         [33.552523, -7.6651507],
         [33.5529399, -7.6652832],
         [33.5542562, -7.6661919],
         [33.5568652, -7.6669174],
         [33.5616769, -7.6689237],
         [33.5618333, -7.6688144],
         [33.5625284, -7.668268],
         [33.5632622, -7.6681107],
         [33.5648033, -7.6678721],
         [33.5697028, -7.6678301],
         [33.5702167, -7.6678147],
         [33.569963, -7.6646805],
         [33.5696344, -7.6624301],
         [33.5697328, -7.6622584],
         [33.5698635, -7.6627064],
         [33.5702491, -7.6661483],
         [33.5711631, -7.6661208],
         [33.5720282, -7.6645927],
         [33.5715266, -7.6641963],
         [33.5718282, -7.6636678],
         [33.572115, -7.6631654],
         [33.5722511, -7.6629269]]

# Marker for starting position in the route:
marker1 = Marker(location=path[0])
m.add_layer(marker1)

# Marker for arriving position in the route:
marker2 = Marker(location=path[len(path)-1])
m.add_layer(marker2)

# Tracker: marker to simulate movement of the vehicle along the path:
tracker = Marker(location=path[0])
m.add_layer(tracker)

# AntPath to display the direction of the path:
ant_path = AntPath(
    locations= path,
    dash_array=[1, 10],
    delay=1000,
    color='#7590ba',
    pulse_color='#3f6fba'
)
# add the AntPath layer:
m.add_layer(ant_path)

# Simulate movement along the path:
for i in range(1, len(path)-1):
    tracker.location = path[i+1]  # update tracker to the next coordinate
    display(m)                    # re-display the map
2
  • Put display(m) outside (before) the loop.
    – TomazicM
    Dec 8 '21 at 16:06
  • It worked ! Didn't know that the position property changes the marker in-place on the original map. Thanks a lot !
    – Metrician
    Dec 8 '21 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.