javascript - Leaflet circle color depend on variable - Stack Overflow

I want the color of the circle in Leaflet to depend on a variable.Current code looks like this and doe

I want the color of the circle in Leaflet to depend on a variable. Current code looks like this and does not work:

              function displayMapLatLng
             (lat, lng, boolean,  displayInfo) {
              var latlng =
              new L.LatLng(lat, lng);

              var color =
                  if (boolean =='Y'){
                                      "blue"
                                      }else{
                                      "red"
                                      }

              var circle =
                  new L.circle ((latlng), 20, {color: 'color', opacity:.5})
                  circle.addTo(map);

Anyone know how to solve this?

I want the color of the circle in Leaflet to depend on a variable. Current code looks like this and does not work:

              function displayMapLatLng
             (lat, lng, boolean,  displayInfo) {
              var latlng =
              new L.LatLng(lat, lng);

              var color =
                  if (boolean =='Y'){
                                      "blue"
                                      }else{
                                      "red"
                                      }

              var circle =
                  new L.circle ((latlng), 20, {color: 'color', opacity:.5})
                  circle.addTo(map);

Anyone know how to solve this?

Share Improve this question asked May 21, 2015 at 7:21 csnakecsnake 1211 gold badge5 silver badges7 bronze badges 1
  • Are you trying to change the fill color or stroke color? – snkashis Commented May 21, 2015 at 13:52
Add a ment  | 

2 Answers 2

Reset to default 4

Your if statement is not valid JavaScript. You can write it as a trenary operation instead:

function displayMapLatLng (lat, lng, boolean,  displayInfo) {
    var latlng = new L.LatLng(lat, lng);
    var color = (boolean === 'Y') ? "blue" : "red";

    var circle = new L.circle ((latlng), 20, {color: color, opacity:.5})
    circle.addTo(map);
}

boolean is a strange name for a string variable too. If it actually is a boolean value you can write the operation as:

var color = (boolean) ? "blue" : "red";

Update:

In the case where you have more possible color values a trenary operation would be less useful. Then an if ... else if statement or switch statement would be more appropriate:

var color;

switch(boolean) {
    case 'B':
        color = 'blue';
        break;
    case 'R':
        color = 'red';
        break;
    case 'W':
        color = 'white';
        break;
    case 'K': // as in kobolt
        color = 'black';
        break;
    default: 
        color = 'yellow';
        break;
}

The default case will be used when no other case matches boolean.

I suspect your code to choose the colour has a syntax error.

If typeof boolean === 'string', try:

var color = boolean == 'Y' ? 'blue' : 'red';

But if, more logically, typeof boolean === 'boolean', try:

var color = boolean ? 'blue' : 'red';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745084096a4610301.html

相关推荐

  • javascript - Leaflet circle color depend on variable - Stack Overflow

    I want the color of the circle in Leaflet to depend on a variable.Current code looks like this and doe

    21小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信