Reputation: 45
Getting ERROR TypeError: Cannot set property 'userCoords' of null error when trying to assign latlng object to a variable
Home.page.ts file
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()
{
}
}
How to assign the value lat and lng to another variable i have to tried to define userCoords as object,String and any
Upvotes: 0
Views: 497
Reputation: 2878
the problem is that function is callback and "this" is current functon context. use arrow function insteed to automaticly bind component "this" to the function
geolocationSuccess = (position) => {
this.userCoords = {
lat: position.coords.latitude, lng:
position.coords.longitude
}
}
Upvotes: 1