While working in a VueJS SPA with javascript, I am attempting to streamline my code by creating a method that passes the Google Maps Places Service a place_id and specified fields for retrieval.
getPlaceDetails (place, fields) {
this.$refs.mapRef.$mapPromise.then((map) => {
var placesServices = new window.google.maps.places.PlacesService(map)
placesServices.getDetails({ placeId: String(place.place_id), fields: fields }, (result, status) => {
if (status === window.google.maps.places.PlacesServiceStatus.OK) {
alert(JSON.stringify(result))
return result
}
})
})
}
The above method is called from within another method:
var place = this.getPlaceDetails(place, ['name', 'geometry', 'place_id'])
The method executes successfully, displaying the desired JSON in the alert, but 'place' comes back as null. I have attempted various approaches such as setting 'vm = this' before defining 'placesServices' and assigning the result to an app level variable like so:
getPlaceDetails (place, fields) {
this.$refs.mapRef.$mapPromise.then((map) => {
var vm = this
var placesServices = new window.google.maps.places.PlacesService(map)
placesServices.getDetails({ placeId: String(place.place_id), fields: fields }, (result, status) => {
if (status === window.google.maps.places.PlacesServiceStatus.OK) {
alert(JSON.stringify(result))
vm.tempPlace = result
}
})
}).then(function () {
return this.tempPlace
})
}
I am struggling to make the method properly return the 'result' object. Any suggestions on how to achieve this?