Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic 2 map loading "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);

      });

    });

  }
like image 776
Dev006 Avatar asked Jun 05 '26 15:06

Dev006


2 Answers

You need to catch the error and do something with it. To do that an error catch needs to be added. The code to fix your problem is here:

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));
        });
    });
}
like image 95
Whitecat Avatar answered Jun 10 '26 18:06

Whitecat


Suggesting you to try to get your error as:

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

You will get proper error message by doing this.

like image 35
Devang Mistry Avatar answered Jun 10 '26 19:06

Devang Mistry