简体   繁体   English

Jvector Map如何从标记添加和获取链接

[英]Jvector Map how to add and get link from marker

I am trying to get data from the marker array and call it back on the onmarkerclick function so I can go to a URL once a marker is clicked, everything I try seems to fail.我试图从标记数组中获取数据并在 onmarkerclick 函数上调用它,这样我就可以在点击标记后转到一个 URL,我尝试的一切似乎都失败了。 I wish to add a URL in to the marker array and return this into the onmarkerclick.我希望在标记数组中添加一个 URL 并将其返回到 onmarkerclick。 Thanks for your help in advanced:感谢您在高级方面的帮助:

 $(function(){
    $('#map1').vectorMap({
                  map: 'world_mill_en',
                  scale: ['#C8EEFF', '#0071A4'],
                  normalizeFunction: 'polynomial',
                  hoverOpacity: 0.7,
                  hoverColor: false,
                  markerStyle: {
                    initial: {
                          fill: '#F8E23B',
                          stroke: '#383f47'
                    }
                  },
                  backgroundColor: '#383f47',
                  markers: [{latLng: [48.921537, -66.829834], name: "something", weburl : "/blah/foo"

            },{latLng: [45.995944, -64.171143], name: "something else", weburl : "/blah/foo"

            },],
                  onMarkerClick: function(events, label, index, weburl) {
                    alert (1+weburl);                
                   }
            });
});

So much coincidence, I faced the same problem yesterday only..:)太巧了,我昨天才遇到同样的问题..:)

The solution I found was to create an array outside, and access it by the index in the click-function..我找到的解决方案是在外部创建一个数组,并通过单击函数中的索引访问它。

var markers = [
    {latLng: [48.921537, -66.829834], name: "something", weburl : "/blah/foo"},
    {latLng: [45.995944, -64.171143], name: "something else", weburl : "/blah/foo-else"}
];

$(function(){
    $('#map1').vectorMap({
                  ...
                  markers: markers,
                  onMarkerClick: function(event, index) {
                      // alter the weburl
                      alert(markers[index].weburl);
                  }
            });
});

Just because I just solved this sort of problem a different way, and I'm feeling very clever for having done so, I will post my answer.仅仅因为我刚刚以不同的方式解决了这类问题,并且我觉得这样做很聪明,所以我会发布我的答案。

You can store arbitrary data using jQuery.data or javascript dom dataSets.您可以使用 jQuery.data 或 javascript dom dataSets 存储任意数据。 Unless you have other SVG on your page with <circle> elements, you can iterate over all <circle> elements and give them data from an array.除非您的页面上有其他带有<circle>元素的 SVG,否则您可以遍历所有<circle>元素并从数组中为它们提供数据。 The indexes will match, but you can use data-index as a safeguard.索引将匹配,但您可以使用 data-index 作为保护措施。

Pretty cool.很酷。 Even though this is way old, maybe this alternative will help someone.尽管这已经很老了,但也许这种替代方法会对某人有所帮助。

Using the solutions from Rajat and Oldenborg I managed to get the marker name using the default jVectorMap setup使用 Rajat 和 Oldenborg 的解决方案,我设法使用默认的 jVectorMap 设置获得了标记名称

onMarkerClick: function(event, index) {

        console.log(map.markers[index].config.name);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM