jVectorMap - 如何从标记获取值并将其存储到变量中

时间:2014-04-14 10:14:15

标签: variables save jvectormap

我设法使用带有标记的jVectorMap创建地图。

现在,如何在用户点击标记时从标记中获取值?

jsfiddle链接:http://jsfiddle.net/fqqGs/78/

     $(function(){
        $('#map').vectorMap({
        map: 'us_aea_en',
        zoomOnScroll: false,
        hoverOpacity: 0.7,
        markerStyle: {
          initial: {
            fill: '#F8E23B',
            stroke: '#383f47'
          }
        },
 markers: [
     {latLng: [41.50, -87.37], name: 'Chicago', newvalue: '300'},
          {latLng: [32.46, -96.46], name: 'Dallas', newvalue: '301'},
          {latLng: [36.10, -115.12], name: 'Las Vegas', newvalue: '302'},
          {latLng: [34.3, -118.15], name: 'Los Angeles', newvalue: '303'},
          {latLng: [40.43, -74.00], name: 'New York City', newvalue: '304'}
        ],
            onMarkerClick: function(event, index){
  var a = 2;
var b=2+newval; //need to get the value from each markers and save the value to newval
alert(b);

 }
        });
    });  

1 个答案:

答案 0 :(得分:1)

将markers数组分配给匿名函数内部的变量,您可以在地图标记的赋值和onMarkerClick闭包内引用该变量。

jsfiddle链接:http://jsfiddle.net/hansenmc/3Cf8r/

   $(function () {
       //assign markers to a variable
       var mapMarkers = [
              {latLng: [41.50, -87.37], name: 'Chicago', newvalue: '300'}, 
              {latLng: [32.46, -96.46], name: 'Dallas', newvalue: '301'}, 
              {latLng: [36.10, -115.12], name: 'Las Vegas', newvalue: '302'}, 
              {latLng: [34.3, -118.15], name: 'Los Angeles', newvalue: '303'}, 
              {latLng: [40.43, -74.00], name: 'New York City', newvalue: '304'}
        ];

       $('#map').vectorMap({
           map: 'us_aea_en',
           zoomOnScroll: false,
           hoverOpacity: 0.7,
           markerStyle: {
               initial: {
                   fill: '#F8E23B',
                   stroke: '#383f47'
               }
           },
           markers: mapMarkers,
           onMarkerClick: function (event, index) {
               var a = 2;
               //retrieve marker by the index and parse the string value as an integer
               var newval = parseInt(mapMarkers[index].newvalue);
               var b = 2 + newval; //need to get the value from each markers and save the value to newval
               alert(b);
           }
       });
   });