In my one of react component,
import React, { Component } from 'react';
import './css/addItem.css';
class AddItem extends Component {
constructor(props) {
super(props);
}
showPosition(position) {
console.log("Latitude: ",position.coords.latitude+
" Longitude: ",position.coords.longitude);
}
getGeoLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
}
render() {
return (
<div>
.....
.....
<button onClick={this.getGeoLocation}>Get GeoLocation</button>
</div>
);
}
}
export default AddItem;
My it says Cannot read property 'showPosition' of undefined
.
GeoLocation is simply not working
.
Being new to React, I tried,
this.showPosition = this.showPosition.bind(this);
in constructor.
But that did not help.
Could some one please explain what am I doing wrong and how to fix it ?