【发布时间】:2021-01-28 17:07:07
【问题描述】:
我在尝试使用 Angular 在 Ionic 页面上显示地图时遇到了一些问题。
虽然地图本身没有错误,但有一些渲染问题让我很恼火:
- 地图的图块加载最初非常缓慢
- 用
setView显示的地图坐标位于地图 div 的左上角(我可以通过放置在相同坐标上的标记看到这一点) - 如果我调整窗口大小,图块会正常加载,并且标记会居中。
我希望第三个选项始终如此:我用setView 指向的坐标应该在地图的中心。
这是我的代码:
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>
【问题讨论】:
标签: angular typescript ionic-framework leaflet