如何从Mongodb中提取数据来制作地图标记
How do I pull data from Mongodb to make a map marker?
我有一个地图,我想在上面做标记。我在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();
}
});
相关文章:
- 如何更改角度谷歌地图上的集群图标
- 谷歌地图API-显示具有不同图标的标记
- 谷歌地图-更改图层图标大小
- 在地图加载/页面加载时,我想将地图设置为当前地理位置,但不想在地图上显示定位图标
- 将图标作为参数传递不会;t工作-谷歌地图
- 谷歌地图隐藏手图标
- 从谷歌地图信息窗口中删除关闭图标
- 在javascript中设置谷歌地图标记图标属性
- 我是否可以在单击特定图标时更改地图标记图标,然后在单击另一个图标时切换,等等
- 我如何才能在某些条件下更改c#中谷歌地图标记的图标
- 使用HTML元素代替gmap3的地图标记图标
- 使用jQuery下拉菜单更改谷歌地图图标
- 使用SVG文件动态修改谷歌地图图标
- 在Javascript中使用相对路径设置谷歌地图图标
- 使谷歌地图图标图像更小
- 自定义谷歌地图图标如果值>=1
- 谷歌地图图标赢得't显示
- 将自定义参数添加到谷歌地图图标中
- 使用精灵自定义谷歌地图图标
- 谷歌地图图标颜色