I'm currently utilizing the below code snippet to retrieve the zip code (pin code) of visitors using the HTML5 geolocation API. However, I have a requirement to extract this zip code and store it in a data variable named 'pincode'. Although the code successfully prints the value in the console, it fails to update the 'pincode' variable.
export default {
data(){
return {
pincode: 0,
}
},
methods: {
findPincode(){
navigator.geolocation.getCurrentPosition(function (position) {
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
geocoder.geocode({
'latLng': latLng
}, function (results, status) {
for (var i = 0; i < results[0].address_components.length; i++) {
var address = results[0].address_components[i];
if (address.types[0] == "postal_code") {
console.log(address.long_name) // prints 680001
this.pincode = Number(address.long_name) // not working
}
}
});
});
}
}
}