首页 > 解决方案 > 我是 node js 和 mongodb 的新手,我想将它应用到带有 Google Maps API 的项目中

问题描述

我正在做一个项目,其中纬度和经度存储在 NoSQL 数据库(MongoDb)中,我希望 Google Maps API 仅获取 bd 中所有文档的这两个值,并将它们分别添加到带有标记的地图中。

<script>

const CASA = 18.177339;
const UNI = 18.074612;

function initMap() {

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
      center: {lat: 18.1778670, lng: -93.1042920}
  });

  // Create an array of alphabetical characters used to label the markers.
  var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  // Textos
  var infoCasa = new google.maps.InfoWindow;
  infoCasa.setContent('<b> ¡Hola! Aqui es mi casa  </b>');

  var infoUni = new google.maps.InfoWindow;
  infoUni.setContent('<b> Y esta es mi uni =)  </b>');

  // Add some markers to the map.
  // Note: The code uses the JavaScript Array.prototype.map() method to
  // create an array of markers based on a given "locations" array.
  // The map() method here has nothing to do with the Google Maps API.
  var markers = locations.map(function (location, i) {

    var marker = new google.maps.Marker({
      position: location,
      label: labels[i % labels.length]
    });

    if (locations[i].lat == CASA) {
      marker.addListener('click', function() {
        infoCasa.open(map, marker);
      });
    } else if (locations[i].lat == UNI) {
      marker.addListener('click', function() {
        infoUni.open(map, marker);
      });
    }
    return marker;
  });

  // Add a marker clusterer to manage the markers.
  var markerCluster = new MarkerClusterer(map, markers,
    { imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' });
}
var locations = [

  { lat: CASA, lng: -93.063082 },
  { lat: UNI, lng: -93.167336 }, 

]
  </script>
  <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
  </script>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=My_Key&callback=initMap">
  </script>
  <script>
</script>

我只有提供 Google Maps API 文档的代码。

我的数据库是这样的: 我的数据库

我需要的是选择数据库中所有集合的纬度和经度以及我拥有的每个类别(agros、palm 等),并且 Google Maps API 将其添加到地图中

标签: node.jsmongodbgoogle-maps

解决方案


推荐阅读