本文介绍了jVectorMap-如何动态添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jVectorMap插件将地图添加到网站.这是我在页面加载中添加标记的地图.有没有办法动态地做到这一点?我需要在鼠标单击上添加它们.我使用jVectorMap插件

I'm using jVectorMap Plugin to add a map to website. Here is a map where I added markers on page load. Is there a way to do it dynamically? I need to add them on mouse click. I use jVectorMap Plugin

   var plants = [
        {name: 'VAK', coords: [-25.274398, 133.775136], status: 'mrk'},
        {name: 'MZFR', coords: [37.090240, -95.712891], status: 'mrk'},
        {name: 'AVR', coords: [50.9030599, 6.4213693], status: 'mrk'}

      ];

   $('#world-map-markers').vectorMap({
    map: 'world_mill_en',
    normalizeFunction: 'polynomial',
    markerStyle: {
        initial: {
            fill: '#F8E23B',
            stroke: '#383f47'
        }
    },
    backgroundColor: '#383f47',
    markers: plants.map(function(h) {
        return {
            name: h.name,
            latLng: h.coords
        }
    }),
    series: {
        markers: [{
            attribute: 'image',
            scale: {
                'mrk': 'marker.png'
            },
            values: plants.reduce(function(p, c, i) {
                p[i] = c.status;
                return p
            }, {}),

        }]
    }
    });
    });

推荐答案

使用空标记和空值初始化地图:

Initialize the map with empty markers and values:

mapObj = new jvm.Map({
    container: $('#world-map-markers'),
    map: 'world_mill_en',
    normalizeFunction: 'polynomial',
    markerStyle: {
        initial: {
            fill: '#F8E23B',
            stroke: '#383f47'
        }
    },
    backgroundColor: '#383f47',
    markers: [],
    series: {
        markers: [{
            attribute: 'image',
            scale: {
                'mrk': 'marker.png'
            },
            values: [],
            }]
    }
});

仅指出您还可以分别设置标记和值,请声明两个数组:

Just only to point out that you can also set markers and values separately, declare two arrays:

var mapMarkers = [];
var mapMarkersValues = [];

然后,在需要点击处理程序的任何地方,调用如下函数:

then, wherever you need the click handler, call a function like this:

function addPlantsMarkers() {
   var plants = [
        {name: 'VAK', coords: [-25.274398, 133.775136], status: 'mrk'},
        {name: 'MZFR', coords: [37.090240, -95.712891], status: 'mrk'},
        {name: 'AVR', coords: [50.9030599, 6.4213693], status: 'mrk'}

   ];
    mapMarkers.length = 0;
    mapMarkersValues.length = 0;
    for (var i = 0, l= plants.length; i < l; i++) {
        mapMarkers.push({name: plants[i].name, latLng: plants[i].coords});
        mapMarkersValues.push(plants[i].status);
    }
    mapObj.addMarkers(mapMarkers, []);
    mapObj.series.markers[0].setValues(mapMarkersValues);
}

最终结果:

这篇关于jVectorMap-如何动态添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 03:53