Closed manjeet87 closed 3 years ago
You have a few syntax errors in you JavaScript code, i think that't why it doesn't work. Here is an example of a JavaScript snippet that creates a marker with a custom icon,
const iconPlane = L.icon({
iconUrl: "/assets/icon_plane.png",
iconSize: [32, 32],
iconAnchor: [16, 16]
});
window.myNamespace = Object.assign({}, window.myNamespace, {
mySubNamespace: {
pointToLayer: function(feature, latlng, context) {
return L.marker(latlng, {icon: iconPlane})
}
}
});
I have just pushed a new version (dash-leaflet==0.1.10
and dash-extensions==0..0.32
) that changes the syntax slightly. Using the new syntax, you could use the icon function like this,
import random
import dash
import dash_html_components as html
import dash_leaflet as dl
import dash_leaflet.express as dlx
from dash_extensions.javascript import Namespace
# Create some markers.
points = [dict(lat=55.5 + random.random(), lon=9.5 + random.random(), value=random.random()) for i in range(100)]
data = dlx.dicts_to_geojson(points)
# Create geojson.
ns = Namespace("myNamespace", "mySubNamespace")
geojson = dl.GeoJSON(data=data, options=dict(pointToLayer=ns("pointToLayer")))
# Create the app.
app = dash.Dash()
app.layout = html.Div([
dl.Map([dl.TileLayer(), geojson], center=(56, 10), zoom=8, style={'height': '50vh'}),
])
if __name__ == '__main__':
app.run_server()
Thanks a lot for your instant response. I tried your implementation, the said error went away... but now it throws a new error:
Cannot read property leafletElement of null
What can be the reason for this?? Should i am required to install specific version. These are my installed version.
dash-leaflet==0.1.5
dash-extensions==0.0.32
The above code is for the latest version, dash-leaflet 0.1.10, try to see if that works.
I am newbie on dash leaflet programing, predominantly python coders, with poor knowledge in javascript. What i wanted is to implement custom icon, as marker, to be displayed for GeoJSON point layer. I followed the following link: http://dash-leaflet.herokuapp.com/#func_props. Instead i wanted to use custom icon for local .png file storred in
/assets/
folder. I have created a icon.js, in my /assets/ folder. Following is the content: `Following is the python code i have used, based on this :
It is throwing error: The function [dash_props.module.point_to_layer] was not found in the global window object.