-1

私は緯度と経度の値を取り出してvar coordsとしてラップすることができるfirebaseデータベースを持っています。他の関数のLatLng変数をGoogleマップに初期化する

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// 
}); 
} 

これはGoogleマップの初期化です。

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

var 'coords'を読み取るには、関数initializeMapが必要です。私はglobalvariableとして 'coords'を入れてみましたが、他の関数の中でgetCoords関数を囲むように試みました。彼らはちょうど読んでいないようです。これを行うための効率的な方法はありますか?

答えて

0

関数を呼び出す必要があります。

私は渡すことで欲しかったものを手に入れることができました...呼び出し側に緯度/長い返すために

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(); 
    return new google.maps.LatLng(latitude, longitude); //modified this line 
}); 
} 

をあなたの機能を変更し、それをここに呼んで...

function initializeMap() { 
var iconBase = {url:'/images/wing.pin.png'}; 
var coords = getCoords(); // called here 

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); 
} 
0

次のようにinitializeMap関数のパラメータとして緯度と経度を設定します。

function getCoords() { 
var plae_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); 

initializeMap(latitude, longitude); 
}); 

} 


    //Load Map in Summary Tab// 
    function initializeMap(latitude,longitude) { 
    var iconBase = {url:'/images/wing.pin.png'}; 


    var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 17, 
    center: new google.maps.LatLng(latitude, longitude), 
    mapTypeId: 'roadmap' 
    }); 

    marker = new google.maps.Marker({ 
    icon: iconBase, 
    map: map, 
    draggable: true, 
    animation: google.maps.Animation.DROP, 
    position: new google.maps.LatLng(latitude, longitude) 
    }); 
    marker.addListener('click', toggleBounce); 
    } 
+0

これはうまくいくかもしれませんが、 'initializeMap'を' get Coordsの機能。 –

+0

私は同意する、あなたは明らかにより流動的です。 :) –