leaflet R, how to make appearance of clustered icon related to statistics of the children?
I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.
It is similar to this problem, which makes icon color of clusters based on count of children. What if I want to customize icon based on the sum of magnitude of earthquakes?
With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction
, as done in this example.
But I am adding marker with addCircleMarkers
and addMarkers
from leaflet for R, and doesn't seem i can add arbitrary attribute to markers being generated. Code below works but it doesn't if i uncomment two lines (mag = ~mag
and sum += markers[i].mag;
)
leaflet(quakes) %>% addTiles() %>% addMarkers(
# mag = ~mag,
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
// sum += markers[i].mag;
sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
I thought about using label=
option of addMarkers
, and then parse it from Javascript. But the markers accessed with getAllChildMarkers()
on marker cluster in JS does not seem to have label
property.
I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?
javascript r leaflet
add a comment |
I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.
It is similar to this problem, which makes icon color of clusters based on count of children. What if I want to customize icon based on the sum of magnitude of earthquakes?
With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction
, as done in this example.
But I am adding marker with addCircleMarkers
and addMarkers
from leaflet for R, and doesn't seem i can add arbitrary attribute to markers being generated. Code below works but it doesn't if i uncomment two lines (mag = ~mag
and sum += markers[i].mag;
)
leaflet(quakes) %>% addTiles() %>% addMarkers(
# mag = ~mag,
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
// sum += markers[i].mag;
sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
I thought about using label=
option of addMarkers
, and then parse it from Javascript. But the markers accessed with getAllChildMarkers()
on marker cluster in JS does not seem to have label
property.
I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?
javascript r leaflet
add a comment |
I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.
It is similar to this problem, which makes icon color of clusters based on count of children. What if I want to customize icon based on the sum of magnitude of earthquakes?
With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction
, as done in this example.
But I am adding marker with addCircleMarkers
and addMarkers
from leaflet for R, and doesn't seem i can add arbitrary attribute to markers being generated. Code below works but it doesn't if i uncomment two lines (mag = ~mag
and sum += markers[i].mag;
)
leaflet(quakes) %>% addTiles() %>% addMarkers(
# mag = ~mag,
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
// sum += markers[i].mag;
sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
I thought about using label=
option of addMarkers
, and then parse it from Javascript. But the markers accessed with getAllChildMarkers()
on marker cluster in JS does not seem to have label
property.
I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?
javascript r leaflet
I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.
It is similar to this problem, which makes icon color of clusters based on count of children. What if I want to customize icon based on the sum of magnitude of earthquakes?
With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction
, as done in this example.
But I am adding marker with addCircleMarkers
and addMarkers
from leaflet for R, and doesn't seem i can add arbitrary attribute to markers being generated. Code below works but it doesn't if i uncomment two lines (mag = ~mag
and sum += markers[i].mag;
)
leaflet(quakes) %>% addTiles() %>% addMarkers(
# mag = ~mag,
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
// sum += markers[i].mag;
sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
I thought about using label=
option of addMarkers
, and then parse it from Javascript. But the markers accessed with getAllChildMarkers()
on marker cluster in JS does not seem to have label
property.
I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?
javascript r leaflet
javascript r leaflet
edited Nov 11 '18 at 18:02
yosukesabai
asked Nov 11 '18 at 5:00
yosukesabaiyosukesabai
4,28611833
4,28611833
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Found my answer. Seems like I can put arbitrary property inside options=
in addMarker
:
leaflet(quakes) %>% addTiles() %>% addMarkers(
options = markerOptions(mag = ~mag),
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
sum += Number(markers[i].options.mag);
// sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything
– Antoine
Nov 24 '18 at 11:19
you have to pass thing like below, two words, seems like.return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing
– yosukesabai
Nov 25 '18 at 4:18
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245984%2fleaflet-r-how-to-make-appearance-of-clustered-icon-related-to-statistics-of-the%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Found my answer. Seems like I can put arbitrary property inside options=
in addMarker
:
leaflet(quakes) %>% addTiles() %>% addMarkers(
options = markerOptions(mag = ~mag),
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
sum += Number(markers[i].options.mag);
// sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything
– Antoine
Nov 24 '18 at 11:19
you have to pass thing like below, two words, seems like.return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing
– yosukesabai
Nov 25 '18 at 4:18
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
add a comment |
Found my answer. Seems like I can put arbitrary property inside options=
in addMarker
:
leaflet(quakes) %>% addTiles() %>% addMarkers(
options = markerOptions(mag = ~mag),
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
sum += Number(markers[i].options.mag);
// sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything
– Antoine
Nov 24 '18 at 11:19
you have to pass thing like below, two words, seems like.return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing
– yosukesabai
Nov 25 '18 at 4:18
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
add a comment |
Found my answer. Seems like I can put arbitrary property inside options=
in addMarker
:
leaflet(quakes) %>% addTiles() %>% addMarkers(
options = markerOptions(mag = ~mag),
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
sum += Number(markers[i].options.mag);
// sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
Found my answer. Seems like I can put arbitrary property inside options=
in addMarker
:
leaflet(quakes) %>% addTiles() %>% addMarkers(
options = markerOptions(mag = ~mag),
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
sum += Number(markers[i].options.mag);
// sum += 1;
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
")
)
)
answered Nov 11 '18 at 7:05
yosukesabaiyosukesabai
4,28611833
4,28611833
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything
– Antoine
Nov 24 '18 at 11:19
you have to pass thing like below, two words, seems like.return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing
– yosukesabai
Nov 25 '18 at 4:18
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
add a comment |
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything
– Antoine
Nov 24 '18 at 11:19
you have to pass thing like below, two words, seems like.return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing
– yosukesabai
Nov 25 '18 at 4:18
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back
,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything– Antoine
Nov 24 '18 at 11:19
great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back
,className: 'marker-cluster'
at the end of the JS function, but it seems we have to re-specify everything– Antoine
Nov 24 '18 at 11:19
you have to pass thing like below, two words, seems like.
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing– yosukesabai
Nov 25 '18 at 4:18
you have to pass thing like below, two words, seems like.
return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40));
you need your logic to set if marker is large/small/medium, like the original code was doing– yosukesabai
Nov 25 '18 at 4:18
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…
– yosukesabai
Nov 25 '18 at 4:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245984%2fleaflet-r-how-to-make-appearance-of-clustered-icon-related-to-statistics-of-the%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown