multiple markers on google maps from firebase database

前端 未结 1 953
深忆病人
深忆病人 2020-12-22 09:17



        
相关标签:
1条回答
  • 2020-12-22 10:06

    You now have multiple positions in your data.

    The code works if you change it to read the data format in your database. You have your coordinates in an array named l in the User object.

    locationsRef.on('child_added', function(snapshot) {
      var data = snapshot.val();
      var marker = new google.maps.Marker({
        position: {
          lat: data.User.l[0],
          lng: data.User.l[1]
        },
        map: map
      });
      bounds.extend(marker.getPosition());
      marker.addListener('click', (function(data) {
        return function(e) {
          infowindow.setContent(this.getPosition().toUrlValue(6) + "<br>" + data.User.g);
          infowindow.open(map, this);
        }
      }(data)));
      map.fitBounds(bounds);
    });
    

    proof of concept fiddle

    code snippet:

    function initialize() {
      var infowindow = new google.maps.InfoWindow();
      var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      // Initialize Firebase
      var config = {
        apiKey: "AIzaSyCWZjRe2CK8Hu2VN35AgZOQ7lQZKcI-UWM",
        authDomain: "carrier-35d7c.firebaseapp.com",
        databaseURL: "https://carrier-35d7c.firebaseio.com",
        projectId: "carrier-35d7c",
        storageBucket: "carrier-35d7c.appspot.com",
        messagingSenderId: "827792028763"
      };
      firebase.initializeApp(config);
    
      //Create a node at firebase location to add locations as child keys
      var locationsRef = firebase.database().ref("locations");
      var bounds = new google.maps.LatLngBounds();
      locationsRef.on('child_added', function(snapshot) {
        console.log(snapshot)
        var data = snapshot.val();
        console.log(data);
        var marker = new google.maps.Marker({
          position: {
            lat: data.User.l[0],
            lng: data.User.l[1]
          },
          map: map
        });
        bounds.extend(marker.getPosition());
        marker.addListener('click', (function(data) {
          return function(e) {
            infowindow.setContent(this.getPosition().toUrlValue(6) + "<br>" + data.User.g);
            infowindow.open(map, this);
          }
        }(data)));
        map.fitBounds(bounds);
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <script src="https://www.gstatic.com/firebasejs/4.12.1/firebase.js"></script>
    <script src="https://cdn.firebase.com/libs/geofire/4.1.2/geofire.min.js"></script>
    <div id="map_canvas"></div>

    0 讨论(0)
提交回复
热议问题