如何确保只在Typescript中完成另一个方法后执行该方法?
我试图在下面找到我的当前位置,然后创建一张地图,使用这些坐标将地图居中:如何确保只在Typescript中完成另一个方法后执行该方法?,typescript,Typescript,我试图在下面找到我的当前位置,然后创建一张地图,使用这些坐标将地图居中: lat; lng; map; ngAfterViewInit(): void { this.getLoc(); this.initMap(); this.markerService.makeCapitalMarkers(this.map); } getLoc() { this.geolocation.getCurrentPosition( { maximumA
lat;
lng;
map;
ngAfterViewInit(): void {
this.getLoc();
this.initMap();
this.markerService.makeCapitalMarkers(this.map);
}
getLoc() {
this.geolocation.getCurrentPosition(
{
maximumAge: 1000, timeout: 5000,
enableHighAccuracy: true
}
).then((resp) => {
this.lat = resp.coords.latitude
this.lng = resp.coords.longitude
}, er => {
//alert("error getting location")
alert('Can not retrieve Location')
});
}
private initMap(): void {
console.log(this.lat + " : " + this.lng);
this.map = L.map('map', {
center: [this.lat, this.lng],
zoom: 3
});
const tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
tiles.addTo(this.map);
}
lat;
液化天然气;
地图;
ngAfterViewInit():void{
这个.getLoc();
this.initMap();
this.markerService.makeCapitalMarkers(this.map);
}
getLoc(){
此文件为.geolocation.getCurrentPosition(
{
最大值:1000,超时值:5000,
EnableHighAccurance:正确
}
)。然后((resp)=>{
this.lat=相应的坐标纬度
this.lng=相应的坐标经度
},er=>{
//警报(“获取位置时出错”)
警报('无法检索位置')
});
}
私有initMap():void{
console.log(this.lat+”:“+this.lng);
this.map=L.map('map'{
中心:[this.lat,this.lng],
缩放:3
});
const tiles=L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'{
maxZoom:19,
属性:“©;”
});
tiles.addTo(this.map);
}
lat
和lng
正在使用正确的值填充,但在创建地图之前不会填充它们
我将它们记录在initMap()
中&它们返回时都是未定义的,但我在HTML中有它们的值,并且它们在HTML中正确显示
我认为问题是地图在坐标读取之前就被加载了。有人能告诉我如何确保在initMap()
之前执行getLoc()
MakegetLoc()
返回承诺,并将initMap()调用成功
比如:
lat;
lng;
map;
ngAfterViewInit(): void {
this.getLoc().then((resp) => {
this.lat = resp.coords.latitude
this.lng = resp.coords.longitude
this.initMap();
this.markerService.makeCapitalMarkers(this.map);
}, er => {
//alert("error getting location")
alert('Can not retrieve Location')
});
}
getLoc() {
return this.geolocation.getCurrentPosition(
{
maximumAge: 1000, timeout: 5000,
enableHighAccuracy: true
}
);
}
private initMap(): void {
console.log(this.lat + " : " + this.lng);
this.map = L.map('map', {
center: [this.lat, this.lng],
zoom: 3
});
const tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
tiles.addTo(this.map);
}
lat;
液化天然气;
地图;
ngAfterViewInit():void{
this.getLoc().then((resp)=>{
this.lat=相应的坐标纬度
this.lng=相应的坐标经度
this.initMap();
this.markerService.makeCapitalMarkers(this.map);
},er=>{
//警报(“获取位置时出错”)
警报('无法检索位置')
});
}
getLoc(){
返回this.geolocation.getCurrentPosition(
{
最大值:1000,超时值:5000,
EnableHighAccurance:正确
}
);
}
私有initMap():void{
console.log(this.lat+”:“+this.lng);
this.map=L.map('map'{
中心:[this.lat,this.lng],
缩放:3
});
const tiles=L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'{
maxZoom:19,
属性:“©;”
});
tiles.addTo(this.map);
}
或者您可以使用async
和wait
使代码更整洁。Hi,感谢您的快速响应。我尝试了上面的代码,但得到了以下控制台错误:无效的LatLng对象:(未定义,未定义)抱歉-错过了this.initMap()之前的几行。我已经编辑了答案。没有完整的样本,我无法真正测试东西,所以它有点拖沓和错误。