2014-04-14 4 views
2

지도에서 마커가있는 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

당신이지도 마커와 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); 
      } 
     }); 
    }); 
관련 문제