2017-10-02 2 views
1

firebaseデータベースから緯度と経度を取得しています。値は変数、つまり緯度と経度に格納されています。しかし、これらの値を使用して地図上のマーカーを設定しようとすると機能しませんでしたが、静的な緯度と場所を指定するとマーカーが表示されます。お願い助けて。ありがとうhtlmとjavaスクリプトでfirebaseデータベースから取得した緯度と経度を使って地図上にマーカーを表示する方法

私は場所を取得し、地図上に表示するために使用するhtmlファイルのコードです。 `

 <head> 
      <title>Data From Firebase Database</title> 
<!--<meta http-equiv="refresh" content="5" > --> 
<style> 
#map{ 

    height: 400px; 
    width: 100%; 
} 
</style> 
</head> 
<body> 


<div class= ""mainDiv" align= "left"> 
<h1> Locations </h1> 
<table> 
<thead> 
<tr> 
<td>Latitude</td> 
<td>Longitude</td> 
</tr> 
</thead> 
<tbody id="table_body"> 
</tbody> 
</table> 
</div> 

<script src= "https://www.gstatic.com/firebasejs/3.3.2/firebase.js"></script> 
<script> 

var config = { 
apiKey: "AIzaSyCU7LWliFjX7iWtNkZHhuZI0jvih6rffFI", 
authDomain: "test26sep-eae46.firebaseio.com", 
databaseURL: "https://test26sep-eae46.firebaseio.com/", 
storageBucket: "test26sep-eae46.appspot.com", 
}; 
firebase.initializeApp(config); 
</script> 

<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
<script> 
var rootRef = firebase.database().ref(); 

rootRef.on("child_added", snap =>{ 

var latitude = snap.child("latitude").val(); 
var longitude = snap.child("longitude").val(); 
initMap(latitude,longitude); 

$("#table_body").append("<tr><td>" + latitude + "</td><td>" + longitude + "</td></tr>"); 

}); 

</script> 

<h1>Map</h1> 
<div id="map"></div> 

    <script> 

function initMap(latitude,longitude){ 


//alert('Values have been gathered click ok to show map !! Welcome Taj , from Arslan !! :PP'); 
var options = { 
    zoom: 8, 
     center: {lat: 31.5546,lng:74.3572} 
} 
var map = new google.maps.Map(document.getElementById('map'), options); 



//show an alert to check if the values are being fetched from firebase database stored in latitude and longitude. 
    alert (latitude + " " + longitude); 


var marker = new google.maps.Marker({ 
     position:{lat: latitude, lng: longitude}, 
     map:map, 
     }); 

     var infoWindow = new google.maps.InfoWindow({ 
     content:'<h4>Lahore</h4>' 
     }); 

     marker.addListener('click', function(){ 
     infoWindow.open(map, marker); 
     }); 

} 
    </script> 

    <script 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD0w2dY7OZvF6nBvnLLxzAzXi05l_8jc-o"> 


    </script> 
</body> 
</html> 

`

+0

'initMap(parseFloatは(緯度)、parseFloatは(経度))を試してみてください;' '.val()は'あなたに文字列として値を返します使用して、私は – duncan

+0

が私@duncan揚げありがとうございましたと思います。出来た。あなたは私の一日を作った。 'position:{lat:parseFloat(latitude)、lng:parseFloat(longitude)}、' –

+0

クール;私は答えとして投稿するよ – duncan

答えて

0

だから、あなたは:

var latitude = snap.child("latitude").val(); 
var longitude = snap.child("longitude").val(); 

は、これらの値は、文字列として.val()から返されています。 Googleマップで作業するには、番号に変換する必要があります。

ちょうど行います。あなたがマップのAPIでそれらを使用

initMap(parseFloat(latitude), parseFloat(longitude)); 

か、を、例えば

position:{lat: parseFloat(latitude), lng: parseFloat(longitude)} 
+0

パーフェクト@ダンカン –

関連する問題