我希望对我的以下javascript代码有一点支持:
// Initialize Firebase
var config = {
apiKey : "AIzaSyBRC2kza6jhghEFNr5dteVpw2kB9mxqrU8",
authDomain : "formulaire-7fba1.firebaseapp.com",
databaseURL : "https://formulaire-7fba1.firebaseio.com",
projectId : "formulaire-7fba1",
storageBucket : "formulaire-7fba1.appspot.com",
messagingSenderId: "548100244430"
};
firebase.initializeApp(config);
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 3,
styles: [{
featureType: 'poi',
stylers: [{ visibility: 'off' }] // Turn off points of interest.
}, {
featureType: 'transit.station',
stylers: [{ visibility: 'off' }] // Turn off bus stations, train stations, etc.
}],
disableDoubleClickZoom: true
});
}
// Loop through users in order with the forEach() method. The callback
// provided to forEach() will be called synchronously with a DataSnapshot
// for each child:
var query = firebase.database().ref("client").orderByKey();
query.once("value").then(function(snapshot) {
var position = [];
snapshot.forEach(function(childSnapshot) {
// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
position.push(childData.lat + " " + childData.lng);
console.log(position);
});
});
我正在尝试将填充GPS位置的数组作为字符串放入谷歌地图,作为标记。尝试了几种方法,但没有一种有效。任何人都可以给我提示或方向吗?
谢谢!
如果position
是保存坐标的数组。您需要确保其中的数组元素遵循latLng
对象或marker.position
属性可以识别的内容。通常它会遵循以下格式:
var latLngObj = {lat: -25.363, lng: 131.044};
您可以使用forEach
循环在每次迭代中添加标记。在将坐标推送到position
数组之前/Aftter执行以下操作:
var marker = new google.maps.Marker({
position: {lat: childData.lat, lng:childData.lng},
map: map
});
我不确定 childData 实际上是什么样子的,因为您没有提供该信息,但假设它是像 -25.363 这样的double
而不是string
,那么它会很好。
您可能还希望全局定义映射变量,以便函数可以在map: map
部分识别它,因为您只在initMap
函数中定义map
变量。
以下文档可指导您如何向地图添加标记。只需使用迭代(例如 for 循环)即可添加多个标记。您还将在同一文档中了解如何删除它们。
还找到了一个关于正确循环以创建标记的相关堆栈溢出帖子。
希望对您有所帮助!