错误TypeError:无法将属性'userCoords'设置为null

时间:2019-08-02 09:49:24

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

获取错误TypeError:尝试将latlng对象分配给变量时,无法设置null错误的属性'userCoords'

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定义为object,String和任何

1 个答案:

答案 0 :(得分:1)

问题在于函数是回调函数,而“ this”是当前函数上下文。使用指示的箭头功能自动将组件“ this”绑定到该功能

<cfoutput>
    <input type="date" class="form-control" name="dtStart" value="#LSDateFormat(now(),'yyyy-mm-dd')#" required />
    <br />
    My Date: #LSDateFormat(now(), 'mm/dd/yyyy')#
</cfoutput>