【问题标题】:ERROR TypeError: Cannot set property 'userCoords' of null错误类型错误:无法将属性“userCoords”设置为 null
【发布时间】:2019-12-10 22:24:40
【问题描述】:

出现错误类型错误:尝试将 latlng 对象分配给变量时,无法设置属性 'userCoords' 的 null 错误

Home.page.ts 文件

 export class HomePage {
  map: GoogleMap;
  userCoords : any ;
   constructor() {

     navigator.geolocation.getCurrentPosition
     (this.geolocationSuccess,this.geolocationError,{});
    this.loadMap();
     }

 loadMap() {

    let mapOptions: GoogleMapOptions = {

           'camera' : {
          target: this.userCoords,
          zoom: 15
  },
  'preferences': {
          'zoom': {
                  'minZoom': 15,
                  'maxZoom': 18
          }
                }
        this.map = GoogleMaps.create('map_canvas', mapOptions);
      }




     geolocationSuccess  = function(position)
      {
     this.userCoords = {lat: position.coords.latitude,lng:    
   position.coords.longitude}  /////===>getting error here
      }
   geolocationError = function()
  {

   }
}

如何将值 lat 和 lng 分配给另一个变量 我必须尝试将 userCoords 定义为对象、字符串和任何

【问题讨论】:

    标签: typescript google-maps cordova ionic-framework ionic4


    【解决方案1】:

    问题是函数是回调,“this”是当前函数上下文。使用箭头函数自动将组件“this”绑定到函数

    geolocationSuccess = (position) => {
      this.userCoords = {
        lat: position.coords.latitude, lng:
          position.coords.longitude
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2014-06-26
      • 2018-01-03
      • 2014-02-07
      • 2014-03-06
      • 1970-01-01
      相关资源
      最近更新 更多