I have a popup for a layer on the Leaflet map and a popup appears when you click on a point on the map. The popup should display a table with the data for that specific layer. However, the popup does not adjust to the table size:
But when I do get rid of the default width for the leaflet-popup-content
the popup does not appear right above the point and gets shifted to the right:
The way I am displaying the popup is the following (in my popup service):
let popupOptions = {
className: "popup",
maxWidth: 250 // This doesn't do anything for some reason
};
L.popup(popupOptions)
.setLatLng(latLng)
.setContent(this.compilePopup(PopupComponent))
.openOn(map);
You can see that I am injecting the PopupComponent into the Leaflet popup rather than just hardcoding the html. Here is how the this.compile
function looks like:
private compilePopup(component: any) {
const compFactory: any = this.resolver.resolveComponentFactory(component);
let compRef: any = compFactory.create(this.injector);
this.appRef.attachView(compRef.hostView);
compRef.onDestroy(() => this.appRef.detachView(compRef.hostView));
let div = document.createElement('div');
div.appendChild(compRef.location.nativeElement);
return div;
and this is how my PopupComponent HTML looks like:
<ng-template>
<table class="table table-striped table-bordered table-condensed table-hover">
<tr>
<th *ngFor="let col of columns;">
{{columns}}
</th>
</tr>
<tr>
<td *ngFor="let col of columns;">
{{columnDetails[columns]}}
</td>
</tr>
</table>
</ng-template>
popup-component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-popup',
templateUrl: './popup.component.html',
styleUrls: ['./popup.component.css']
})
export class PopupComponent implements OnInit {
columnDetails: any;
columns: Array<string> = ["Column 1", "Column 2", "Column 3"];
constructor(
private popupService: PopupService,
private popupStore: PopupStore
) {
this.columnDetails= this.popupService.columnDetails;
}
}
and the way I am initializing my Leaflet map is in my map-component.ts where I have:
options: MapOptions = {
center: latLng(47.5786262, -122.1654623),
minZoom: 4,
layers: [
L.gridLayer.googleMutant({
type: 'roadmap', // valid values are 'roadmap', 'satellite', 'terrain' and 'hybrid'
styles: [
{
featureType: "poi.business",
elementType: "labels",
stylers:
[
{
visibility: "simplified"
}
]
}
]
}),
],
zoom: 5,
zoomControl: false
};
map-component.html:
<div id="map"
leaflet [leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
(leafletMapMove)="onMapMove($event)"
(leafletMapZoom)="onMapZoom($event)"
(leafletClick)="onMapClick($event)">
</div>
Now I found another question with the exact same problem as mine and saw that you can set the maxWidth: "auto"
or use update()
, but these two solutions do not work for me since that's specifically for Leaflet javascript and I'm using ngx-leaflet, angular leaflet.
How can I get it so the popup adjust to my table size AND that the popup is right above the marker that I click on? or is there a way to translate the other solution to my code? Any help is greatly appreciated!