簡體   English   中英

角谷歌地圖在組件加載時變得不確定

[英]Angular google map getting undefined lat long on components load

我正在使用離子角AGM。 我正在嘗試獲取組件加載的當前位置。 但是似乎它在加載之前首先觸發了組件,這就是為什么它不讀取我的經度和緯度的原因。 如何使它像反應式的?

export class DriverMapComponent implements OnInit {
  coordinates;

constructor(
    private navCtrl: NavController,
    private http: HttpClient,
    private route: ActivatedRoute,
    private storage: Storage
  ) {
    //I also tried here but not working
  }

ionViewWillEnter(){
   //not working
}

ngOnInit() {
    this.storage.get("sr_details").then(params => {
      if (params) {
        this.coordinates = {
          lat: params.pickuplat,
          long: params.pickuplong
        };
        console.log(this.coordinates);
      }
    });
  }

AGM

<ion-content>
  <agm-map
    [zoom]="15"
    [latitude]="coordinates.lat"
    [longitude]="coordinates.long"
  >
    <!-- <agm-direction
          *ngIf="address.place !== '' && destination.palce !== ''"
          [origin]="ori"
          [destination]="des"
        ></agm-direction> -->
    <agm-marker
      [latitude]="coordinates.lat"
      [longitude]="coordinates.long"
    ></agm-marker>
  </agm-map>
</ion-content>

一種解決方案是,以這種方式將子組件包裝在*ngIf='coordinates'使子組件僅在擁有所需數據后才呈現(調用ngOnInit方法)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM