javascript - how to bind the following to the local this context variable on button click event? -


i need on this. how print lat , long using button click event like:

<button id="geo" onclick="ongeoclick()"> location </button> 

i have following code:

var getgeo = {     showposition  :      function showposition(position) {         self = this;         console.log(self)         self.lat = position.coords.latitude;         self.lng = position.coords.longitude;         console.log(self);     },       showerror :     function showerror(error) {         switch(error.code) {             case error.permission_denied:                 this.err = "user denied request geolocation.";                 break;             case error.position_unavailable:                 this.err = "location information unavailable.";                 break;             case error.timeout:                 this.err = "the request user location timed out.";                 break;             case error.unknown_error:               this.err = "an unknown error occurred.";               break;         }     },      getlocation :     function getlocation() {             self = this;             if (navigator.geolocation) {                 navigator.geolocation.getcurrentposition(self.showposition, self.showerror, {enablehighaccuracy:true});                 console.log(self);             } else {                  self.err = "geolocation not supported browser.";}     } };  function ongeoclick() {...local context binding?...} 

such window not store lat , long accessible variable can log(result) out;

you can use bind method keep context intact.

navigator.geolocation.getcurrentposition(self.showposition.bind(self), self.showerror, {enablehighaccuracy:true}); 

and call this

function ongeoclick() {    getgeo.getlocation(); } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -