Question

I have a rather large script to generate my map using the gmap3 plugin. In the plugin, normally you would apply multiple map markers like follows. I know this works when applied with static data.

marker:{
    values:[
        {address:"555 Anywhere Rd Port Clinton, OH", data:'<h4>Some Data 1</h4>'},
        {address:"556 Anywhere Rd Port Clinton, OH", data:'<h4>Some Data 2</h4>'},
        {address:"557 Anywhere Rd Port Clinton, OH", data:'<h4>Some Data 3</h4>'} 
    ]
}

The problem starts when I try to pass the array markerValues outside an each loop and it's returning empty.

here is my full code:

/****************************************
*** DEALER LOCATOR MAP
****************************************/
(function($){
    jQuery(document).ready(function(){
        var tableRows = [];
        var headersText = [];
        var $headers = $("th");
        var markerValues = {};
        var $rows = $("#table tbody tr").each(function(index) {
          $cells = $(this).find("td");
          tableRows[index] = {};
          $cells.each(function(cellIndex) {
            if(headersText[cellIndex] === undefined) {
              headersText[cellIndex] = $($headers[cellIndex]).text();
            }
            tableRows[index][headersText[cellIndex]] = $(this).text();
          });    
        });
        var tableData = {
            "tableData": tableRows
        };
        $(function(){
           $.each(tableData["tableData"], function(key, value){
                markerValues = {
                   address: value.STREET+" "+value.CITY+","+value.STATE+" "+value.ZIP,
                   data: '<p>'+value.NAME+'<br />'+value.STREET+'<br />'+value.CITY+', '+value.STATE+'<br /> '+value.ZIP+'<br />'+value.TEL+'</p>' 
                };
            }); 
        });      

        console.log( markerValues );

        $(function(){ 
            $('#map_canvas').gmap3({ 
              map:{
                options:{
                  zoom: 3,
                  center: [41.374774, -83.651323], 
                  streetViewControl: true,
                  mapTypeId: google.maps.MapTypeId.TERRAIN,
                  mapTypeControl: true,
                  mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                  }
                }
              },
              marker:{
                values:[markerValues],
                options:{
                  draggable: false,
                  /*icon: "/static/images/mapmarker.png"*/
                },
                events:{
                  click: function(marker, event, context){
                      $(this).gmap3({
                        clear:"overlay"
                      });
                      $(this).gmap3({
                        overlay:{
                            latLng: marker.getPosition(),
                            options:{
                              content: '<div id="small_infowindow"><div class="row-fluid"><div class="span12">'+(context.data)+'</div></div></div>',
                              offset:{
                                y: -30,
                                x: 30
                              }
                            }
                        }
                      });
                      $(this).gmap3('get').panTo(marker.getPosition());
                  },
                  dblclick: function() {
                    $(this).gmap3({
                        clear:"overlay"
                    });
                  }
                },
              }
            });
        });
    });
})(jQuery);
Was it helpful?

Solution

the variable is not accesible outside of the loop

$.each(tableData["tableData"], function(key, value){
    var markerValues = {
        address: value.STREET+" "+value.CITY+","
          +value.STATE+" "+value.ZIP,
        data: '<p>'+value.NAME+'<br />'+value.STREET+'<br />'
          +value.CITY+', '+value.STATE+'<br /> '+value.ZIP+'<br />'
          +value.TEL+'</p>' 
    };
    console.log( markerValues );
}); 

You should define it (using the "var") outside of the loop, just before the 9th line

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top