gpt4 book ai didi

javascript - Uncaught ReferenceError : mapInitialised is not defined

转载 作者:行者123 更新时间:2023-12-03 12:42:47 25 4
gpt4 key购买 nike

$(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;
}
});

我在“mapInit”函数内收到错误Uncaught ReferenceError: mapInitialized is not Defined

我仍在学习,不确定我做错了什么。

编辑:这是我在此应用程序中使用的完整代码:

$(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);
});
}

最佳答案

“mapInitialized”不是仅在声明它的函数范围内吗?我认为一旦你修复了该变量的范围,你就会遇到与“mapInit()”中使用的其他变量类似的问题,比如“homeLatitude”。

关于javascript - Uncaught ReferenceError : mapInitialised is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com