0
$(document).ready(function(){

// Initialise global variables
var geoJson = getMarkers();
var homeLatitude = 50.351;
var homeLongitude = -3.576;
var initialZoom = 15;
var mapInitialised = 0;

// Initialise map
var map = mapInit();

function mapInit() {
    // initialise map
    if ( mapInitialised===0 ) {
        var map = L.mapbox.map('map', 'mapIdentifier.234grs')
            .setView([homeLatitude, homeLongitude], initialZoom);
        mapInitialised = 1;
    }
    return map; 
}
});

I get the error Uncaught ReferenceError: mapInitialised is not defined inside the 'mapInit' function.

I'm still learning here and am unsure of what I'm doing wrong.

Edit: here's the whole code that I'm using in this app:

$(document).ready(function(){

    // Initialise global variables
    var geoJson = getMarkers();
    var homeLatitude = 50.351;
    var homeLongitude = -3.576;
    var initialZoom = 15;
    var mapInitialised = 0;

    // Initialise map
    var map = mapInit();

    // Add features to map
    var myLayer;
    addFeaturesToMap(map, myLayer);

    // Define map behaviour
    myLayer.on('click', function(e) {
        e.layer.openPopup();
    });

    // define button behaviour for switching between map and list
    $('#buttonMap').click(function(){
        switchToMap();
    });

    // define button behaviour for switching between map and list
    $('#buttonList').click(function(){
        switchFromMap();
    });

    $('.view-on-map').click(function(e){

        // Scroll to the top of the map
        $(document).scrollTo(0,500);

        // get lat, long, and name for event location
        var latitude = $(this).attr("data-latitude");
        var longitude = $(this).attr("data-longitude");
        var name = $(this).attr("data-name");

        // go to map
        switchToMap();

        // show popup
    var popup = L.popup()
            .setLatLng([latitude, longitude])
            .setContent('<p><strong>' + name + '</strong></p>' )
            .openOn(map);

    }); // end of view-on-map click function

}); // end of document ready function

function getMarkers() {

    // define geoJson object
    var geoJson = {
        type: 'FeatureCollection',
        features: []
    };

    var arrayLength = events.length;

    // loop through events array and push into geoJson object
    for (var i = 0; i < arrayLength; i++) {
        var latitude = events[i].location[0].latitude;
        var longitude = events[i].location[0].longitude;
        var name = events[i].location[0].name;
        var pageId = events[i].location[0].pageId;
        var link = events[i].location[0].link;
        var description = events[i].location[0].description;

        var feature = {
            type: 'Feature',
            properties: {
                title: name,
                other: "text",
                'marker-color': '#54a743',
                'stroke': '#428334',
                url: link
            },
            geometry: {
                type: 'Point',
                coordinates: [longitude, latitude]
            }
        }

        // push to geoJson.features array
        geoJson.features.push(feature); 
    }
    return geoJson;
}

function switchToMap() {
    // show map
    $('#map').fadeIn();
    // change button states
    $('#buttonMap').attr("disabled", "disabled");
    $('#buttonList').removeAttr("disabled");
}

function switchFromMap() {
    // hide map
    $('#map').fadeOut();
    // change button states
    $('#buttonList').attr("disabled", "disabled");
    $('#buttonMap').removeAttr("disabled");
}

function mapInit() {
    // initialise map
    if ( mapInitialised===0 ) {
        var map = L.mapbox.map('map', 'mapIdentifier.234grs')
            .setView([homeLatitude, homeLongitude], initialZoom);
        mapInitialised = 1;
    }
    return map; 
}

function addFeaturesToMap(map, myLayer) {

    // declare feature layer
    myLayer = L.mapbox.featureLayer().addTo(map);

    // Pass features and a custom factory function to the map
    myLayer.setGeoJSON(geoJson);

    // set template for popup
    myLayer.eachLayer(function(layer) {

  var content = 'Location:' + layer.feature.properties.title;
    layer.bindPopup(content);
    });
}
babbaggeii
  • 7,577
  • 20
  • 64
  • 118

1 Answers1

1

Isn't "mapInitialised" only in the scope of the function in which it is declared? I think once you fix the scoping of that variable, you will then run into similar issues with other variables used in "mapInit()", like "homeLatitude".

jordan
  • 959
  • 7
  • 17