离子Google地图找不到元素[#map_canvas]

时间:2019-04-19 10:19:23

标签: angular google-maps ionic4

我有一个使用Google Maps Polyline的Ionic App,可从数据json API获取经纬度和经度,以获取飞行路线。我想自动重新加载数据,以便在地图上查看实时数据。 l添加了设置间隔以重新加载数据。数据已重新加载并进入控制台,但是我在下面有错误,地图上没有实时数据。

ERROR Error: Uncaught (in promise): Error: Can not find the element [#map_canvas]
Error: Can not find the element [#map_canvas]
    at vendor.js:76400
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2781)
    at Object.onInvokeTask (vendor.js:51333)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2780)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2553)
    at push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (polyfills.js:2856)
    at ZoneTask.invoke (polyfills.js:2845)
    at timer (polyfills.js:4639)
    at resolvePromise (polyfills.js:3189)
    at resolvePromise (polyfills.js:3146)
    at polyfills.js:3250
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2781)
    at Object.onInvokeTask (vendor.js:51333)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2780)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2553)
    at drainMicroTaskQueue (polyfills.js:2959)
    at push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (polyfills.js:2860)
    at ZoneTask.invoke (polyfills.js:2845)

我的代码

 ngOnInit() {
    this.platform.ready();
    this.getmarker();

setInterval(()=> {
  this.getmarker()
},4000); 

  }

  async getmarker() {
    this.http.get('xxxxxxx.json?flightId=4232343, {}, {})

      .then(data => {

        for (let datas of JSON.parse(data.data).result.response.data.flight['track']) {
          this.points.push({ lng: datas.longitude, lat: datas.latitude });
        }
        let AIR_PORTS = this.points;
        console.log(AIR_PORTS)

        this.map = GoogleMaps.create('map_canvas', {
          camera: {
            target: AIR_PORTS
          }
        });

        let polyline: Polyline = this.map.addPolylineSync({
          points: AIR_PORTS,
          color: '#AA00FF',
          width: 5,
          geodesic: true,
          clickable: true,


        });

        let points: Array<ILatLng> = this.points = []



      })
  }

html

  <div style="height: 100%;width: 100%" id="map_canvas"></div>

1 个答案:

答案 0 :(得分:1)

您可以显示整个html吗?请确保父元素具有一定的高度。 CSS始终将百分比值视为父元素的百分比。

通常此问题是由分配0px高度引起的。以这种方式考虑,map div在初始化时为空,父元素的高度为0px。稍后填充地图内容时,我们将看不到任何东西,因为父级0px的高度100%为0px。

<ion-content>
    <ion-card style="height: 50%">
        <div style="height: 100%;width: 100%" id="map_canvas"></div>
    </ion-card>
</ion-content>

确保父元素(即上例中的离子卡)在初始化时具有一定高度。您可以设置一个固定的测试高度,这将证明我的观点。

<div style="height: 200px;" id="map_canvas"></div>

祝你好运。