离子 2 地图加载"Uncaught (in promise): [object PositionError]"


 initMap(): Promise<any> {
    this.mapInitialised = true;//part 1
    return new Promise((resolve) => {

      this.geolocation.getCurrentPosition().then((position) => {// Part 2
   let latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
        let mapOptions = {
          center: latLng,
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        this.map = new google.maps.Map(this.mapElement, mapOptions);
        resolve(true);
      });
    });
  }

您需要捕获错误并对其进行处理。为此,需要添加错误捕获。修复您的问题的代码在这里:

initMap(): Promise<any> {
    this.mapInitialised = true;//part 1
    return new Promise((resolve) => {
        this.geolocation.getCurrentPosition().then(
            (position) => {// Part 2
            let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            let mapOptions = {
                center: latLng,
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP;
            }
            this.map = new google.maps.Map(this.mapElement, mapOptions);
            resolve(true);
        },
        // Here is the error catching that needs to be added
        err =>{
             console.log(' Error : ' + JSON.stringify(error));
        });
    });
}

建议你尝试将错误理解为:

this.geolocation.getCurrentPosition().then(
(position) => {  
    /* your code */
} err =>{
      alert('Error message : '+ err.message);
 });

通过这样做,您将获得正确的错误消息。

相关内容

  • 没有找到相关文章

最新更新