jVectorMap error: “jvm is not defined”

谁都会走 提交于 2019-12-05 06:11:23

I had this exact same problem. The problem is the ZIP file you downloaded that redirects you to

<script src="../jquery-jvectormap.js"></script>

Is actually a JS file that invokes JVM, not the actual JVM library (Which is why you're getting the "JVM Is not Defined" error.

The way I fixed it was to take the file

http://jvectormap.com/js/jquery-jvectormap-1.2.2.min.js

and include it in my project.

That's the ACTUAL JVM library, so as long as that's included before you make any .vectorMap calls, it'll work out just great for you.

So you are using not minified js file. It contains only main code without libraries.

To fix this error you have 2 solutions:

  1. Add all needed libraries. You can find it in lib/ directory. Example with files and with which order you need to add you find in tests/index.html file

  2. Create minified js. You need to use NIX system and execute ./build.sh. Maybe you will need to install uglifyjs utility, for example you can install npm from here and then execute npm install uglify-js@1

It appears the minified file contains all the various files needed while jquery-jvectormap.js does not. So if you want to use the non-minified version then you should be prepared to manually load all the various files needed by jquery-jvectormap.js for example map.js, vector-canvas.js, e.t.c. These can be found in /src directory if you get your files from the jvector maps website

The statesValues, metroPopValues, and metroUnempValues variable declarations should be their own statement terminated with a ;. You currently end the lines with a ,.

var val = 2009;
var statesValues = jvm.values.apply({}, jvm.values(data.states));
var metroPopValues = Array.prototype.concat.apply([], jvm.values(data.metro.population));
var metroUnemplValues = Array.prototype.concat([], jvm.values(data.metro.unemployment));

If you still want to save the correct files locally, for example if you are using bower, then you can use this "built" version of jvectormap also on GitHub.

If you downloaded the zip or cloned from GitHub: Go to the directory where you downloaded and execute the build.sh file. That will generate the minified file for you to use with all of the proper dependencies.

On Mac: source build.sh

Or you can just use npm Install the dependency: npm install jvectormap --save and then use the minified file in you node_modules.

Donno much about jVectorMap but a simple observation.

In your code, the variable 'jvm' is nowhere defined. Wouldn't it be a good idea to check the JS files used in the code and look out for any initialization of the 'jvm' variable ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!