admin管理员组文章数量:1291009
I'm aiming to have my markers in three different colours depending on their rating
property. I have seen a similar post where an object was used to define the colours. Each marker has a rating
attribute between 1 and 5.
I was thinking of using a else if statement e.g
if (rating < 3) {
markerColor: 'red'
} else if (rating = 3 ) {
markerColor: 'orange'
} else {
markerColor: 'green'
}
I am creating my markers as follows:
for (var i = 0; i < data.length; i++) {
var customOptions = {
'maxWidth': '500',
'className' : 'custom'
};
//Custom icon
var blueMarker = L.AwesomeMarkers.icon({
markerColor: 'blue'
});
//Create markerLocation variable
var markerLocation = new L.LatLng(data[i].lat, data[i].lon);
//Create marker variable
var marker = new L.Marker(markerLocation, {icon: blueMarker});
marker.bindPopup("<p><h2>Rating:</h2> " + data[i].rating_value,
customOptions);
}
Would the else if statement be used when assigning the blueMarker variable?
Thanks
I used
var = customColour = "green";
if (data[i].rating_value < 3)
customColor = "red";
else if (data[i].rating_value === 3)
customColor = "orange";
//Create custom icon
var customMarker = L.AwesomeMarkers.icon({
markerColor: customColour
});
//Create markerLocation variable
var markerLocation = new L.LatLng(data[i].lat, data[i].lon);
//Create marker variable
var marker = new L.Marker(markerLocation, {icon: customMarker});
However the AwesomeMarkers plugin only accepts colours, so I dont think using customColour worked. .awesome-markers. Thanks
I'm aiming to have my markers in three different colours depending on their rating
property. I have seen a similar post where an object was used to define the colours. Each marker has a rating
attribute between 1 and 5.
I was thinking of using a else if statement e.g
if (rating < 3) {
markerColor: 'red'
} else if (rating = 3 ) {
markerColor: 'orange'
} else {
markerColor: 'green'
}
I am creating my markers as follows:
for (var i = 0; i < data.length; i++) {
var customOptions = {
'maxWidth': '500',
'className' : 'custom'
};
//Custom icon
var blueMarker = L.AwesomeMarkers.icon({
markerColor: 'blue'
});
//Create markerLocation variable
var markerLocation = new L.LatLng(data[i].lat, data[i].lon);
//Create marker variable
var marker = new L.Marker(markerLocation, {icon: blueMarker});
marker.bindPopup("<p><h2>Rating:</h2> " + data[i].rating_value,
customOptions);
}
Would the else if statement be used when assigning the blueMarker variable?
Thanks
I used
var = customColour = "green";
if (data[i].rating_value < 3)
customColor = "red";
else if (data[i].rating_value === 3)
customColor = "orange";
//Create custom icon
var customMarker = L.AwesomeMarkers.icon({
markerColor: customColour
});
//Create markerLocation variable
var markerLocation = new L.LatLng(data[i].lat, data[i].lon);
//Create marker variable
var marker = new L.Marker(markerLocation, {icon: customMarker});
However the AwesomeMarkers plugin only accepts colours, so I dont think using customColour worked. https://github./lvoogdt/Leaflet.awesome-markers. Thanks
Share Improve this question edited Apr 25, 2017 at 17:02 Steven asked Apr 25, 2017 at 14:30 StevenSteven 1791 gold badge1 silver badge5 bronze badges 1-
Is it a typo
customColor
vscustomColour
? – xmojmr Commented Apr 26, 2017 at 5:34
2 Answers
Reset to default 4You'd use the conditional statement to determine the value which gets passed into the markerColor
property. In my example below, I store the determined colour within a variable named customColor
:
var customColor = "green";
if (rating < 3)
customColor = "red";
else if (rating === 3)
customColor = "orange";
var blueMarker = L.AwesomeMarkers.icon({
markerColor: customColor
});
I would use a function so you can easily add more keys in the future. It would look something like:
function determineColor(rating) {
if ( rating < 3) {
return 'red';
} else if ( rating === 3 ) {
return 'orange';
} else {
return 'blue';
}
}
var Marker = L.AwesomeMarkers.icon({
markerColor: determineColor(rating)
});
Also reminder one =
assigns values to variables and ===
checks for equality.
本文标签: javascriptChanging marker colour based on property in LeafletStack Overflow
版权声明:本文标题:javascript - Changing marker colour based on property in Leaflet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506995a2382380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论