JQVMap Click Function

This is a very simple question, and I am not a professional on the Internet. I need to create an interactive map. I am using JQVMap. Now I need to click the area and it will refer to the status URL. I give and carry out the functions given as an example on the site. But I do not know how to configure the link with the state and URLs.

jQuery(document).ready(function() { jQuery('#vmap').vectorMap({ map: 'usa_en', backgroundColor: '#ffffff', color: '#333333', hoverColor: '#af090f', selectedColor: '#0076a3', enableZoom: true, showTooltip: true, scaleColors: ['#C8EEFF', '#006491'], onRegionClick: function(element, code, region) { var message = 'You clicked "' + region + '" which has the code: ' + code.toUpperCase(); alert(message); } }); }); 
+4
source share
1 answer

The onRegionClick callback contains everything you need to create a dynamic URL. It returns the click event itself, a 2-digit code representing the area you clicked, and the full name of the region you clicked on. For example, if you used the USA map and clicked on the state of Colorado, you will return (regionClickEvent, "co", "Colorado")

In the first example, I built a dynamic url from a region that was clicked and redirected you there. If the name or status code is not part of the URL, you can do something like the second example, where you check in which area you clicked and process it accordingly.

You will need to update the url to suit your needs, but hopefully this will give you an idea.

* * ex. Use the area to create the URL on the fly * *

 $('#vmap') .vectorMap({ map: 'usa_en', onRegionClick: function (event, code, region) { window.location.replace("http://geology.com/state-map/" + region.toLowerCase() + ".shtml"); } }) 

* * ex. Check the region that was clicked and redirected based on some other rules * *

 $('#vmap') .vectorMap({ map: 'usa_en', onRegionClick: function (event, code, region) { switch (code) { case "co": window.location.replace("http://www.google.com"); break; case "ca": window.location.replace("http://www.yahoo.com"); break; case "tx": window.location.replace("http://www.bing.com"); break; } }, onRegionOver: function (event, code, region) { document.body.style.cursor = "pointer"; }, onRegionOut: function (element, code, region) { document.body.style.cursor = "default"; } }) 

In the updated course material ... if you do not want to do this in every state, you can do the same check as in onRegionClick, and check the code and see if this is the state that you want to show what the cursor style is on.
Or, if there is a state in which you do not want to appear as clickable, you can kill the event before it fires like this.

 onRegionOver: function (event, code, region) { if (code == "tx") event.preventDefault(); else document.body.style.cursor = "pointer"; }, 

In any case, I hope this helps.

+5
source

Source: https://habr.com/ru/post/1434079/


All Articles