javascript - Add markers from firebase to google map in website - Stack Overflow

Firebase data structure : I need to add markers in the google map using these latlang.My code : <scr

Firebase data structure :



I need to add markers in the google map using these latlang.

My code :

    <script>
    var map;

    function initMap() {

        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 6.930274, lng: 79.861618},
          zoom: 16.5,
          panControl: false,
          gestureHandling: 'greedy',
          disableDefaultUI: true,
          disableDoubleClickZoom: true,
        });

        var dbRef= firebase.database().ref('bins');
        dbRef.on('value', function(snapshot) {
            var snap = snapshot.val();
            var marker = new google.maps.Marker({
              position: {lat: snap.lat, lng: snap.lang},
              map: map
            });
        });
    }
</script>

Nothing happened when this code runs.

Firebase data structure :



I need to add markers in the google map using these latlang.

My code :

    <script>
    var map;

    function initMap() {

        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 6.930274, lng: 79.861618},
          zoom: 16.5,
          panControl: false,
          gestureHandling: 'greedy',
          disableDefaultUI: true,
          disableDoubleClickZoom: true,
        });

        var dbRef= firebase.database().ref('bins');
        dbRef.on('value', function(snapshot) {
            var snap = snapshot.val();
            var marker = new google.maps.Marker({
              position: {lat: snap.lat, lng: snap.lang},
              map: map
            });
        });
    }
</script>

Nothing happened when this code runs.

Share Improve this question edited Jun 23, 2018 at 0:48 Nuwan Alawatta asked Mar 23, 2018 at 5:49 Nuwan AlawattaNuwan Alawatta 1,86721 silver badges29 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

check this out

use parseFloat() in the position because firebase return data as strings.

position: {lat: parseFloat(snap.lat), lng: parseFloat(snap.lang)}

This is because you are not accessing the children of bins you have to use forEach to be able to get the values:

 var dbRef= firebase.database().ref('bins');
    dbRef.on('value', function(snapshot) {
    snapshot.forEach(function(child) {
    var childs=child.val();
    var marker = new google.maps.Marker({
          position: {lat: childs.lat, lng: childs.lang},
          map: map
          });
       });
   });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745397983a4625967.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信