简体   繁体   English

角谷歌地图在组件加载时变得不确定

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

I'm using Ionic-angular AGM. 我正在使用离子角AGM。 I'm trying to get my current location on components load. 我正在尝试获取组件加载的当前位置。 But it seems like its firing the component first before it loads that's why its not reading my latitude and longitude. 但是似乎它在加载之前首先触发了组件,这就是为什么它不读取我的经度和纬度的原因。 How to make it like reactive? 如何使它像反应式的?

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 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