node.js - 我是 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 将其添加到地图中
解决方案
推荐阅读
- xamarin.forms - 我们可以在 Visual Studio 或 Android Studio IDE 中构建基于 Xamarin 的 Android 系统应用程序吗?
- excel - 如何使 VBA 函数将 If, then 语句的结果输出到单元格?
- javascript - 在 Typescript 中返回动态编译时间类型?
- c# - EnableCorsAttribute 中缺少方法
- r - 带有 Observes 和 reactiveValues 的闪亮模块
- ruby-on-rails - ChatRooms#index 中的 ExecJS::RuntimeError
- sorting - 将日期行分隔为月初和月底 (LibreOffice)
- python - Python:指令后更新 GUI
- python-3.x - 在 bash 中运行的脚本的日志输出显示为一行
- python - Flask:视图函数没有返回有效的响应元组