I'm having some issues trying to show a map on a page in Ionic using Angular.
While the map itself works without bugs, there are a few rendering issues that annoy me:
- The map's tile loading is initially extremely slow
- The coordinates which the map is meant to be showing with
setView
are in the top left corner of the map div (I can see this with a marker placed on the same coords) - If I resize the window, the tiles load normally as they should and the marker is centered.
I'd like the third option to be the case at all times: the coordinates I point to with setView
should be at the center of the map.
Here is my code as it is:
export class MyPage implements OnInit {
public acc;
posLatitude: number;
posLongitude: number;
map: L.Map;
markerIcon = L.icon(
{
iconUrl:'assets/pin.svg',
iconSize: [32, 32],
}
);
constructor(
private myService: MyService,
) { }
ngOnInit() {
this.acc = MyService.getAcc();
this.posLongitude = this.acc["Long"];
this.posLatitude = this.acc["Lat"];
this.map = L.map('map').setView([this.posLatitude, this.posLongitude], 16);
// center the map
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'jsp.com'
}).addTo(this.map);
L.marker([this.posLatitude, this.posLongitude], {icon : this.markerIcon}).addTo(this.map)
.bindPopup('Accident');
console.log(this.acc);
}
}
<div id="map" style="height: 75%;"></div>