如何从Mongodb中提取数据来制作地图标记

How do I pull data from Mongodb to make a map marker?

本文关键字:地图 图标 数据 Mongodb 提取      更新时间:2023-09-26

我有一个地图,我想在上面做标记。我在Mongodb中做了一个集合,并有一个样本数据来做标记。我不知道如何获取数据并制作标记。

地图

Meteor.startup(function() {  
  GoogleMaps.load();
});
Template.map.helpers({  
  mapOptions: function() {
    if (GoogleMaps.loaded()) {
      return {
        center: new google.maps.LatLng(-37.8136, 144.9631),
        zoom: 8
      };
    }
  }
});
Template.map.helpers({
  marker: function() {
    return MarkerData.find();
   }
});

制作标记

for (var i = 0; i < MarkerData.length; i++) {
  var lat = {{ lat }}
  var lng = {{ lng }}
  var title = {{ title }}
  var address = {{ address }}
  var url = {{ url }}
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng (lat, lng),
    map: map,
    title: title,
    animation: google.maps.Animation.DROP,
  });
}

我的收藏

MarkerData = new Mongo.Collection ('markerdata');    

这是集合中的数据

if (MarkerData.find().count() === 0) {
  MarkerData.insert({
    lat: 34.200659,
    lng: -118.519986,
    title: 'Extensions Plus HQ',
    address: '17738 Sherman Way Reseda, CA 91335',
    url: 'http://extensionsplus.com'
  }); 
} 

还有更多数据要输入。这只是一个确保代码正常工作的示例。

幸运的是,我最近在一个应用程序中实现了类似的功能。我发现,在Meteor中,即使你只有20个左右的标记,在谷歌地图上渲染一组标记也很容易出错。因此,我在每次渲染之间使用500毫秒的延迟,这使得代码比它应该的更复杂。以下是根据你的代码修改的完整工作代码:

Template.map.onCreated(function () {
  var self = this;
  // Create the map.
  self.state = new ReactiveDict('myTemplate.state');
  self.state.set('mapMarkers', []);
  // Initialize map
  GoogleMaps.ready('myMap', function (map) {
    self.state.set('mapReady', true);
  });
});
Template.map.onRendered(function(){
  var self = this;
  // Push markers to UI when ready
  // XXX Google Maps does not behave well when you try to add markers all at
  //      once and will throw errors. Therefore, rather than rendering markers
  //      on the map upon creation, here we first push them to an array, then
  //      in an autorun we separately render them with a delay.
  GoogleMaps.ready('myMap', function onMapReady(map) {
    // setup dependencies
    var isMapReady = self.state.get('mapReady');
    var hasData = !!MarkerData.findOne();
    var isGoogleDefined = typeof(google) !== 'undefined';
    var theMarkers = MarkerData.find({}, {sort: {state:1}});
    var allMarkers = [];
    var infoWindows = [];
    if ( isMapReady && hasData && isGoogleDefined ) {
      var markers = theMarkers.fetch();
      var marker, docMarker, markers, infoWindow;
      // Render marker for each item in collection
      _.each(markers, function (marker, i, cursor) {
        var latitude = marker.lat;
        var longitude = marker.lng;
        var latlng = new google.maps.LatLng( latitude, longitude );
        // Build the info window to contain marker details.
        infoContent = '<strong>' + marker.title + '</strong>' +'<br/>' + marker.address;
        // Render it
        infoWindow = new google.maps.InfoWindow({
          content: infoContent,
          disableAutoPan: true
        });
        // Create marker to place on map
        marker = new google.maps.Marker({
          position: latlng
        });
        // Push marker to UI state
        allMarkers.push(marker);
        infoWindows.push(infoWindow);
        self.state.set('mapMarkers', markers);
      });
    }
    // Ensure the array we created contains all markers
    // from our collection, then render them with a delay.
    self.autorun(function () {
      var markerLength = allMarkers.length;
      var targetLength = theMarkers.count();
      if ( markerLength === targetLength ) {
        _.each( allMarkers, function( marker, index, list ){
          Meteor.setTimeout(function(){
            var mapInstance = GoogleMaps.maps.myMap.instance;
            // This is what actually places the marker on the map.
            marker.setMap( mapInstance );
            google.maps.event.addListener(marker, 'click', function () {
              infoWindows[index].open(mapInstance, marker);
            });
            google.maps.event.addListener(marker, 'mouseout', function () {
              infoWindows[index].close();
            });
          }, 500);
        });
      }
    });
  });
});
Template.map.helpers({
  mapOptions: function () {
    // Ensure the API is loaded
    if ( GoogleMaps.loaded() ) {
      // initialization opts
      return {
        center: new google.maps.LatLng(-37.8136, 144.9631),
        zoom: 8
      }
    } else {
      return null;
    }
  },
  allMarkers: function () {
    return MarkerData.find();
  }
});