One more solution without using any plugins. I am reading the data from the Mongo DB and then use Javascript, Angular(not necessaril ) and Leaflet libraries to draw the marker for the coordinates that we get from the Mongo DB.
HTML
Just displaying the leaflet div and the onclick event where we pass the date and email of a user. This information will be passed to the MongoDB to fetch the set of latitudes and longitudes of the user for the given time frame
<div class="map" leaflet [leafletOptions]="historicalOptions">
<div *ngFor="let l of historicalLayers" [leafletLayer]="l"></div>
</div>
<div class="col-lg-3 d-flex justify-content-center align-items-center">
<button type="button" (click)="getHistoricalData(onDate,fromTime,toTime,user.email)" class="btn btn-primary p-4">
Get Map
</button>
</div>
Code
ngOnInit() {
this.historicalOptions = {
layers: [
tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '...' }),
this.heatmapLayer
],
center: latLng(xxxxxxx, xxxxxxx),
zoom: 16
};
}
public getHistoricalData(onDate: Date, fromTime: Date, toTime: Date, userEmail) {
this.historicalLayers = [];
this.apiService.getGPSLocations(fromDate.toISOString(), toDate.toISOString(), userEmail)
.subscribe(data => {
if (data && data.length) {
const markerLayers = this.drawMarkerPlotline(data, 'blue');
this.historicalLayers = markerLayers;
}
}
}
private drawMarkerPlotline(data, color) {
if (data) {
let rawData;
if (typeof data === 'string')
rawData = JSON.parse(data);
else
rawData = data;
const layers = [];
if (rawData.length) {
this.heatmapData.data = [];
rawData.forEach((point, index) => {
if (point && point.sensorValue && point.sensorValue.attr_lat && point.sensorValue.attr_lng) {
const markerLayer = marker([point.sensorValue.attr_lat, point.sensorValue.attr_lng], {
icon: icon({
iconSize: [15, 15],
iconAnchor: [7, 7],
iconUrl: this.getIconPath(color)
}),
title: new Date(point.observationTimestamp).toLocaleString(),
});
markerLayer.userId = point.userAccount.userId;
layers.push(markerLayer);
this.heatmapData.data.push({
lat: point.sensorValue.attr_lat,
lng: point.sensorValue.attr_lng,
// count: 1
});
}
});
this.heatmapLayer.setData(this.heatmapData);
return [layerGroup(layers)];
}
}
return null;
}
private getIconPath(color) {
let icon: string;
switch (color) {
case 'green':
icon = 'assets/green-circle.png'
break;
case 'orange':
icon = 'assets/orange-circle.png'
break;
default:
icon = 'assets/blue-circle.png'
break;
}
return icon;
}
Note
Code snippets are directly taken from my sample application. Hence make changes wherever required in order to suit your requirements.