[Vuejs]-Using Google Geocode results outside of Vue.js function

0👍

Your question is mainly about this. Changing function as an arrow function will help to change the this correctly.

var geocoder = new window.google.maps.Geocoder();
                var address = "Dublin";
               geocoder.geocode( { 'address': address}, (results, status) => {
                if (status == window.google.maps.GeocoderStatus.OK) {
                  var latitude = results[0].geometry.location.lat();
                  var longitude = results[0].geometry.location.lng();
                  console.log(latitude) //returns correct data
                  console.log(longitude) //returns correct data
                  this.me = longitude
                          } 
                  }); 
                console.log(this.me) //returns nothing!!
 console.log(longitude) //returns nothing!!

I did not try to run the code, as this is part of the code, but it should work.

Try reading this for details. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Leave a comment