【问题标题】:If statetement inside class类内的if语句
【发布时间】:2016-01-18 14:57:10
【问题描述】:

我正在尝试使用 Google Maps API 创建医院地图。圆的半径是该医院的床位数。另外,我想为不同类型的医院设置不同的颜色。这可以通过 IF 或 SWITCH 语句来完成,但都不起作用。这里我附上 IF 语句的例子。

问题在于声明 if (citymap[city].tip = 1)(在下面注释掉)。

我确定问题是在这个地方错误地使用了函数。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Circles</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

var citymap = {
  KBC_ZAGREB: {
    center: {lat: 45.823554, lng: 16.005904},
    beds: 1975,
    contentString: "KBC ZAGREB, 1975 kreveta",
    tip: 1
  },
  KBC_SPLIT: {
    center: {lat: 43.503908, lng: 16.457924},
    beds: 1521,
    contentString: "KBC SPLIT, 1521 kreveta",
    tip: 1
  },
  KBC_RIJEKA: {
    center: {lat: 45.332623, lng: 14.425665},
    beds: 1191,
    contentString: "KBC RIJEKA, 1192 kreveta",
    tip: 1
  },
  KBC_OSIJEK: {
    center: {lat: 45.558230, lng: 18.711740},
    beds: 1160,
    contentString: "KBC OSIJEK, 1160 kreveta",
    tip: 1
  },
   KBC_SESTRE_MILOSRDNICE: {
    center: {lat: 45.815438, lng: 15.953599},
    beds: 1207,
    contentString: "KBC SESTRE MILOSRDNICE, 1207 kreveta",
    tip: 1
  },
  KB_DUBRAVA: {
    center: {lat: 45.834369, lng: 16.035823},
    beds: 625,
    contentString: "KB DUBRAVA, 625 kreveta",
    tip: 2
  },
  KB_MERKUR: {
    center: {lat: 45.820832, lng: 15.997447},
    beds: 345,
    contentString: "KB MERKUR, 345 kreveta",
    tip: 2
  },
  KB_SVETI_DUH: {
    center: {lat: 45.820140, lng: 15.938853},
    beds: 554,
    contentString: "KB SVETI DUH, 554 kreveta",
    tip: 2
  }

};

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: {lat: 45.811076, lng: 15.979270},
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  for (var city in citymap) {

    var cityCircle = new google.maps.Circle({

        strokeColor: '#FF0000',
        fillColor: '#FF0000',

//    if (citymap[city].tip = 1){
//      strokeColor: '#FF0000',
//      fillColor: '#FF0000'
//      } else if (citymap[city].tip = 2){
//      strokeColor: '#3333cc',
//      fillColor: '#3333cc'
//      },

      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillOpacity: 0.35,
      map: map,
      position: citymap[city].center,
      center: citymap[city].center,
      radius: Math.sqrt(citymap[city].beds) * 10
    });

    var infowindow = new google.maps.InfoWindow({
        content: citymap[city].contentString
    });

    cityCircle.addListener('click', function() {
        infowindow.open(map, cityCircle);
    });

  }
}

    </script>
    <script async defer
        src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script>
  </body>
</html>

【问题讨论】:

  • citymap[city].tip = 1 将值1 设置为citymap[city].tip,使用===== 进行比较

标签: javascript html if-statement switch-statement


【解决方案1】:

单等号= 用于分配值,如果要比较,可以使用双等号== 比较两个值或三等号=== 来检查这些比较值的类型,在您的情况下,您可以使用双标或三标:

if(citymap[city].tip == '1')
//OR
if(citymap[city].tip === '1'))

希望这会有所帮助。

【讨论】:

  • 尝试使用console.log(citymap[city].tip)在控制台中显示变量,以确保它返回一些东西。
  • 是的,它返回1或2,取决于citymap[city].tip的值
  • 好的尝试通过在1 中添加引号将其作为字符串进行比较,检查我的更新。
  • :) 试试console.log(citymap[city].tip == 1, citymap[city].tip == '1'),看看它会返回什么。
  • 同时,我对代码进行了更改。我创建了分离函数function createCircle(map, position,center,radius, data, tip) 并在那里添加了这个条件。现在它起作用了。感谢您的帮助!
【解决方案2】:

使用

if (citymap[city].tip == 1)

检查。

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 2021-09-06
    • 2017-07-11
    • 2022-01-19
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    相关资源
    最近更新 更多