【问题标题】:MAP_READY not fired on AndroidMAP_READY 未在 Android 上触发
【发布时间】:2018-07-22 04:27:55
【问题描述】:

自从我将 Google 地图移到另一个页面后,我在使用 Ionic 应用程序时遇到了问题。它从未出现在根/主页上。在 iOS 上,地图显示得很好,但在 Android 上,从未触发 MAP_READY 事件,因此在下面的示例中,我没有看到“地图已准备好”。

ionViewDidLoad() {
    this.platform.ready().then(() => {
        console.log('Platform ready');
        this.loadMap();
    });
}

loadMap() {

    let mapOptions: GoogleMapOptions = {
        camera: {
            target: {
                lat: this.lat,
                lng: this.lon
            },
            zoom: 5
        },
        controls: {
            myLocationButton: true,
            compass: true
        }
    };

    this.map = GoogleMaps.create('map_canvas', mapOptions);

    console.log('Map created');

    this.map.one(GoogleMapsEvent.MAP_READY)
        .then(() => {

            console.log('Map is ready!');
        }, (err) => {
            console.log("Error is ",err);
            }
        );
)

【问题讨论】:

  • promise 捕获错误了吗?
  • 我添加了 .catch 但也没有运行。

标签: google-maps ionic-framework


【解决方案1】:

结果很简单。纬度和经度值尚未设置。在 Android 上,这意味着地图从未宣布准备好,在 iOS 上,它在 (0,0) 处显示地图。我需要做的就是从GoogleMapOptions 中删除相机属性,并在我拥有真实值时设置它们

 let mapOptions: GoogleMapOptions = {
    controls: {
        myLocationButton: true,
        compass: true
    }
};

【讨论】:

    【解决方案2】:

    尝试使用HtmlElement 而不是ID

      <div #maps id="maps">
    
      @ViewChild('maps') mapElement;
      ionViewDidLoad() {
        this.map = GoogleMaps.create(this.mapElement.nativeElement);
      }
    

    【讨论】:

    • 没有变化。我已将错误捕获代码添加到示例中。
    • 你检查设备控制台日志了吗?
    • 是的。在 iOS 上,它显示所有三个消息,“平台准备就绪”、“地图已创建”和“地图准备就绪”。在 Android 上,它只显示前两个。
    猜你喜欢
    • 2019-05-09
    • 2010-12-22
    • 2012-01-01
    • 1970-01-01
    • 2021-07-24
    • 2014-01-16
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多