将标记从Firebase添加到网站中的Google Map [英] Add markers from firebase to google map in website

查看:67
本文介绍了将标记从Firebase添加到网站中的Google Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase数据结构:



我需要使用这些latlang在Google地图中添加标记.



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

我的代码:

    <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.

推荐答案

这是因为您没有访问 bins 的子级,因此必须使用 forEach 才能获取值:

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

这篇关于将标记从Firebase添加到网站中的Google Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆