javascript谷歌地图v3应用程序帮助-说明顺序
基本上,我正在为我的几个朋友制作一个漂亮而简单的移动网络应用程序。它使用一些在线数据库来存储商店的位置数据。我的数据库工作得很好。没问题。事实上,一切都在运转,只是我认为一切都是以错误的顺序发生的。数据库中的数据应该存储在一个数组中,然后该数组中的对象显示在屏幕上。然而,使用一些控制台日志,我发现数据正在显示,然后从数据库中检索,然后填充数组。但不管我做什么,我都不能让它工作!这是我的密码:javascript谷歌地图v3应用程序帮助-说明顺序,javascript,google-maps,Javascript,Google Maps,基本上,我正在为我的几个朋友制作一个漂亮而简单的移动网络应用程序。它使用一些在线数据库来存储商店的位置数据。我的数据库工作得很好。没问题。事实上,一切都在运转,只是我认为一切都是以错误的顺序发生的。数据库中的数据应该存储在一个数组中,然后该数组中的对象显示在屏幕上。然而,使用一些控制台日志,我发现数据正在显示,然后从数据库中检索,然后填充数组。但不管我做什么,我都不能让它工作!这是我的密码: var latOfSpots; var lngOfSpots; var nameOfS
var latOfSpots;
var lngOfSpots;
var nameOfSpots;
var spotArray;
var spotLatLng;
var spotCollection;
var markers;
var Spot;
var spot;
function init() {
//-------------------------- INITIATE SPOT VARIABLES ---------------------------//
map = new google.maps.Map2(document.getElementById("map"));
latOfSpots= new Array(51.14400,51.02295);
lngOfSpots= new Array(0.25721,0.26450);
nameOfSpots= new Array('Tescos', 'Sainsburys');
spotLatLng= new Array();
markers= new Array();
Spot = Parse.Object.extend("Spot");
spot = new Spot();
//----------------- GET DATA FROM THE PARSE.COM DATABASE ---------------------//
//---------------------- DISPLAY ARRAY DATA ON MAP ---------------------------//
GetData();
DisplayData();
//----------------------- SET MAP SETTINGS -----------------------------------//
map.setCenter(spotLatLng[0],8);
//map.addControl(new google.maps.LargeMapControl());
map.addControl(new google.maps.MapTypeControl());
}; //END OF INIT FUNCTION ------------------------------------------------//
google.setOnLoadCallback(init);
//------------------- PRIMARY FUNCTION TO GET DATA FROM DATABASE ---------------//
function GetData()
{
var query = new Parse.Query(Spot);
spotCollection = query.collection();
spotCollection.fetch({
success: function(spotCollection) {
// spotCollection.toJSON()
// will now be an array of objects based on the query
FillArrays();
console.log('data retreived' + spotCollection);
}
});
}
//----------------- FUNCTION TO LOAD DATABASE INTO ARRAYS -------------------//
function FillArrays()
{
spotArray = spotCollection.toJSON();
for (var j = 0; j<spotArray.length; j++)
{
latOfSpots.push(spotArray[j].Latitude);
lngOfSpots.push(spotArray[j].Longitude);
nameOfSpots.push(spotArray[j].Name);
}
}
//------------------------ FUNCTION TO DISPLAY ALL ARRAY DATA ONSCREEN -----------------//
function DisplayData()
{
for(var i = 0; i<latOfSpots.length; i++)
{
spotLatLng[i] = new google.maps.LatLng(latOfSpots[i], lngOfSpots[i]);
for(var x = 0; x<latOfSpots.length; x++)
{
markers[x] = new google.maps.Marker(
spotLatLng[i], {
"draggable":false,
"title":nameOfSpots[i],
});
map.addOverlay(markers[x]);
}
}
console.log('data displayed');
}
var-latofspot;
var lngOfSpots;
点的var名称;
阵列;
var spotLatLng;
变量集;
var标记;
var点;
var点;
函数init(){
//--------------------------启动SPOT变量-----------------//
map=新的google.maps.Map2(document.getElementById(“map”);
latOfSpots=新阵列(51.14400,51.02295);
lngOfSpots=新阵列(0.25721,0.26450);
nameOfSpots=新数组('Tescos','Sainsburys');
spotLatLng=新阵列();
markers=新数组();
Spot=Parse.Object.extend(“Spot”);
spot=新spot();
//-----------------从PARSE.COM数据库获取数据---------------------//
//----------------------在地图上显示阵列数据-----------------//
GetData();
显示数据();
//-----------------------设置地图设置-----------------------------------//
地图设置中心(spotLatLng[0],8);
//addControl(新的google.maps.LargeMapControl());
addControl(新的google.maps.MapTypeControl());
}; //初始化函数结束------------------------------------------------//
setOnLoadCallback(init);
//-------------------从数据库获取数据的主要函数---------------//
函数GetData()
{
var query=newparse.query(Spot);
spotCollection=query.collection();
spotCollection.fetch({
成功:功能(spotCollection){
//toJSON()
//现在将是一个基于查询的对象数组
填充数组();
console.log('数据检索'+spotCollection);
}
});
}
//-----------------函数将数据库加载到数组中-------------------//
函数FillArrays()
{
spotArray=spotCollection.toJSON();
对于(var j=0;j您的数据库查询是异步的。您需要使用Get_数据回调函数中的数据(在它从服务器返回后)。当前,您正在尝试在服务器返回之前使用它
//------------------- PRIMARY FUNCTION TO GET DATA FROM DATABASE ---------------//
function GetData()
{
var query = new Parse.Query(Spot);
spotCollection = query.collection();
spotCollection.fetch({
success: function(spotCollection) {
// spotCollection.toJSON()
// will now be an array of objects based on the query
FillArrays();
console.log('data retreived' + spotCollection);
DisplayData();
}
});
}