-1
我有一个firebase数据库,我可以检索经度和纬度值并将其包裹为var coords。从另一个函数传递LatLng变量到谷歌地图初始化
function getCoords() {
var place_data= firebase.database().ref("/place/name");
place_data.once('value').then(function(snapshot) {
var longitude = snapshot.child("Event_long").val();
var latitude = snapshot.child("Event_lat").val();
var coords = new google.maps.LatLng(latitude, longitude);
alert(coords); //i get (43.6672568, -79.4000838) as alert//
});
}
这是谷歌地图初始化。
function initializeMap() {
var iconBase = {url:'/images/wing.pin.png'};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: coords,
mapTypeId: 'satellite'
});
marker = new google.maps.Marker({
icon: iconBase,
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
position: coords
});
marker.addListener('click', toggleBounce);
}
我只是需要函数initializeMap来读var'coords'。我试过把'coords'作为全局变量,试图在其他函数中包含getCoords函数。他们似乎没有阅读。任何有效的方法来做到这一点?
虽然这可能会起作用,但从get调用'initializeMap'坐标功能。 –
我同意,你的显然更流畅。 :) –