我在驾驶时使用Map来显示骑手。Map在Map上以标记的形式显示位置。但是在mongodb
中更新数据后,我的Map没有更新。我使用forestadmin和node js。在forestadmin中,我使用smartview,我必须使用 ember.js。我显示leaflet OpenStreetMap
Map。
下面是我正在使用的javascript:
export default Component.extend(SmartViewMixin, {
store: service(),
tagName: "",
map: null,
loaded: false,
init(...args) {
this._super(...args);
this.loadPlugin();
},
didInsertElement() {
this.displayMap();
},
onRecordsChange: observer("records", function () {
this.displayMap();
}),
loadPlugin() {
scheduleOnce("afterRender", this, () => {
$.getScript(
"//cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js",
() => {
$.getScript(
"//cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js",
() => {
this.set("loaded", true);
this.displayMap();
}
);
}
);
const headElement = document.getElementsByTagName("head")[0];
const cssLeafletLink = document.createElement("link");
cssLeafletLink.type = "text/css";
cssLeafletLink.rel = "stylesheet";
cssLeafletLink.href =
"//cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css";
headElement.appendChild(cssLeafletLink);
const cssDrawLink = document.createElement("link");
cssDrawLink.type = "text/css";
cssDrawLink.rel = "stylesheet";
cssDrawLink.href =
"//cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css";
headElement.appendChild(cssDrawLink);
});
},
displayMap() {
if (!this.loaded) {
return;
}
if (this.map) {
this.map.off();
this.map.remove();
this.map = null;
}
const markers_arr = [];
this.records.forEach(function (record) {
markers_arr.push([
record.get("forest-latitude"),
record.get("forest-longitude"),
record.get("id"),
record.get("forest-userId.forest-email"),
record.get("forest-userId.forest-bikeModel"),
record.get("forest-userId.forest-bikeRegNo"),
record.get("forest-userId.forest-phone"),
]);
});
var map = L.map("map").setView(
new L.LatLng(markers_arr[0][0], markers_arr[0][1]),
7
);
const osmUrl =
"https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png";
const osmAttrib =
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>';
const osm = new L.TileLayer(osmUrl, { attribution: osmAttrib });
map.addLayer(osm);
var markers = [];
markers_arr.forEach(function (mark) {
var foo = "ID: "+mark[2] + "\n <br>Email: " + mark[3]+ "\n <br>Phone: " + mark[6]+ "\n <br>Bike Model: " + mark[4] + "\n <br>Bike #: " + mark[5]
var marker1 = L.marker([mark[0], mark[1]] )
.addTo(map)
.bindPopup(foo);
markers.push(marker1);
marker1.on("mouseover", function (ev) {
marker1.openPopup();
});
});
function markerFunction(id) {
for (var i in markers) {
var markerID = markers[i].options.title;
if (markerID == id) {
markers[i].openPopup();
}
}
}
},
});
下面是HTML:
<style>
#map {
width: 100%;
height: 100%;
z-index: 4;
}
</style>
<div id="map"></div>
我也在HTML中使用fetchRecord
,但它不更新最新数据。
<button {{action 'fetchRecords'}}>
Refresh data
</button>
帮帮我🙏🏻🙏🏻🙏🏻
1条答案
按热度按时间o75abkj41#
这是因为当你的记录改变时,你没有重新计算你的Map。首先,如果你想让它自动更新,我会在你的情况下做的是每秒检索记录或类似的事情,这样你就可以做下面这样的事情,但我不确定它会解决你的问题。
"奇怪的是,你每次取数据都在破坏Map,刷新标记不行吗?这可能是你的问题。“
第一个