I typically like to move things to python if i can help it
I’d suggest using the dynamic google maps loader
see here for what to add to native libraries:
Maps JavaScript API را بارگیری کنید | Google for Developers
Native Library code
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "API KEY",
v: "weekly",
// Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.).
// Add other bootstrap parameters as needed, using camel case.
});
</script>
then i’d simplify your html for form 3 to be just
<div anvil-name="map" style="height: 400px; width: 100%;"></div>
finally i would move the logic to python
see code
from ._anvil_designer import Form3Template
from anvil import *
import anvil.js
from anvil.js.window import google
import random
colors = ["red", "blue", "yellow", "orange", "brown"]
routes = [
{
"origin": "amsterdam",
"destination": "amsterdam",
"waypoints": ["schiphol amsterdam", "schevingen", "rotterdam", "utrecht", "utrecht", "zwolle"],
"travelMode": "DRIVING",
},
{
"origin": "breda",
"destination": "breda",
"waypoints": ["eindhoven", "maastricht", "haaren", "venlo", "eindhoven"],
"travelMode": "DRIVING",
},
]
# ensure maps is loaded
maps = google.maps.importLibrary("maps")
class Form3(Form3Template):
def __init__(self, oplossing=None, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
self.map = google.maps.Map(
self.dom_nodes["map"],
{
"center": {"lat": -34.397, "lng": 150.644},
"zoom": 8,
},
)
self.oplossing = oplossing or routes
self.create_routes()
def calculate_and_display_routes(self, service, renderer, route):
waypoints = [{"location": location, "stopover": True} for location in route["waypoints"]]
try:
result = service.route(
{
"origin": route["origin"],
"destination": route["destination"],
"waypoints": waypoints,
"optimizeWaypoints": True,
"travelMode": route["travelMode"],
}
)
renderer.setDirections(result)
except Exception as e:
Notification(f"Direction request failed due to {e}", style="danger").show()
def create_routes(self):
for route in self.oplossing:
service = google.maps.DirectionsService()
renderer = google.maps.DirectionsRenderer(
{
"map": self.map,
"polylineOptions": {
"strokeColor": random.choice(colors), # Willekeurige kleur
"strokeOpacity": 0.7,
},
}
)
self.calculate_and_display_routes(service, renderer, route)
you’ll probably need to add functionality like being able to clear the routes
but i don’t know the Maps api, you’d have to go look at the docs
clone: