【问题标题】:Randomly colored US map using JVector使用 JVector 随机着色的美国地图
【发布时间】:2013-04-30 00:08:18
【问题描述】:

我在编码美国地图时遇到问题,它允许使用 JVector API 为美国各州地图随机分配颜色。代码如下:

<html>
  <script src="scripts/jquery-1.8.2.js"></script>
  <script src="scripts/jquery-jvectormap-1.2.2.min.js"></script>
  <script src="scripts/jquery-jvectormap-us-aea-en.js"></script>
<body>
  <div  id="us-map" style="position: relative;   width: 800px; height: 600px"></div>
  <script>

  <!--// I commented out this piece of script. It works fine. This is a test trial to load the map
    // $(function(){
    // $('#us-map').vectorMap({map: 'us_aea_en'});
    // });
    -->

    <!-- I have issues with the following function -->
/*it does not even load the map. What it should do is to generate random colors
* for the map as the "update" button is pressed 
*/
$(function(){
  var palette = ['#66C2A5', '#FC8D62', '#8DA0CB', '#E78AC3', '#A6D854'];
      generateColors = function(){
        var colors = {},
            key;

        for (key in map.regions) {
          colors[key] = palette[Math.floor(Math.random()*palette.length)];
        }
        return colors;
      },
      map;

  map = new jvm.USMap({
    map: 'us_aea_en',
    container: $('#map'),
    series: {
      regions: [{
        attribute: 'fill'
      }]
    }
  });
  map.series.regions[0].setValues(generateColors());
  $('#update-colors-button').click(function(e){
    e.preventDefault();
    map.series.regions[0].setValues(generateColors());
  });
})
    </script>
    </div>
  </body>
</html>

这是我的scripts folder 的链接,我在其中保存了.js 文件。 function() 有什么问题?

【问题讨论】:

  • 您是否尝试过在没有“随机颜色”功能的情况下加载地图?
  • @Kamil 是的,我已经在上面的代码中注释掉了那个块。您可以复制粘贴整个代码并下载 `scripts 文件夹,然后尝试运行它。您会看到,如果您取消注释用于简单加载地图的代码并注释掉“随机颜色”功能,它会正常工作。地图加载正常。你能帮我实现“随机颜色”功能吗?
  • 你为什么有', map;'在 generateColors() 结束时?
  • 您的问题使用的代码来自:jvectormap.com/examples/random-colors。不幸的是,他们的示例缺少某些内容,因此您需要直接从页面源复制/粘贴。我更新了我的答案以用正确的代码反映这一点。
  • @Ringo 他有',map',因为它是一个变量声明,既声明了一个函数,又声明了一个空变量。就像你在 c/java 中做 Object a,b,c,d,e 一样,除了在这种情况下,它是在我第一次看到的函数定义之后添加的。

标签: javascript map jvectormap


【解决方案1】:

您的问题与直接从http://jvectormap.com/examples/random-colors/复制的代码有关

使随机美国地图工作的代码是这样的:

取自他们的来源,只有(取自他们的来源,只有地图:更改为美国)。

<html>
  <script src="scripts/jquery-1.8.2.js"></script>
  <script src="scripts/jquery-jvectormap-1.2.2.min.js"></script>
  <script src="scripts/jquery-jvectormap-us-aea-en.js"></script>
<body>
  <div  id="map" style="position: relative;   width: 800px; height: 600px"></div>

 <script>
      //@code_start
      $(function(){
        var palette = ['#66C2A5', '#FC8D62', '#8DA0CB', '#E78AC3', '#A6D854'];
            generateColors = function(){
              var colors = {},
                  key;

              for (key in map.regions) {
                colors[key] = palette[Math.floor(Math.random()*palette.length)];
              }
              return colors;
            },
            map;

        map = new jvm.WorldMap({
          map: 'us_aea_en',
          container: $('#map'),
          series: {
            regions: [{
              attribute: 'fill'
            }]
          }
        });
        map.series.regions[0].setValues(generateColors());
        $('#update-colors-button').click(function(e){
          e.preventDefault();
          map.series.regions[0].setValues(generateColors());
        });
      })
      //@code_end
    </script>   
    </div>
  </body>
</html>

以下错误:

在第一次检查时生成:

Error: ReferenceError: map is not defined
Source File: file:///D:/xampp_october_28_2011/htdocs/stackoverflow/scripts/map.html
Line: 30

在尝试使用之前,您还没有 map 变量。 此外,您的代码似乎隐藏了需要修复的各种其他错误。

还发布到 Dropbox:https://dl.dropboxusercontent.com/u/6465647/mapRandom.html

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2016-03-21
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多