I have a component with a section tag with an id and ref of map
. The id is used to load a Google Map, don't worry about this.
<section
id="map"
ref="map"
>
</section>
I have a function called showUserLocationOnTheMap()
with a button called Map
that's invoked when a User clicks on it. This function shows a marker on the map based on an address, that is stored in the db from a previous step.
showUserLocationOnTheMap(latitude, longitude) {
let map = new google.maps.Map(document.getElementById("map"), {
zoom:15,
center: new google.maps.LatLng(latitude, longitude),
mapTypeId:google.maps.MapTypeId.ROADMAP
});
const marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
map: map
});
google.maps.event.addListener(marker, "click", () => {
const infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(
`<div class="ui header">
<h6>company name</h6>
</div>
`
);
infoWindow.open(map, marker);
});
this.$refs.map.scrollToTop();
},
<button
@click="showUserLocationOnTheMap(item.profile.latitude,item.profile.longitude)"
class="apply-job-btn btn btn-radius btn-primary apply-it"
>
Map
</button>
What I'm trying to do is, I have a bunch of search results, so when someone is scrolling far down the page and they click the map button from the record, it will do two things.
- It shows a marker on the map (This is working)
- It scrolls back up to the top of the page so the User can see the Map with the marker on it.
WHAT I TRIED: I created this function below:
scrollToTop() {
this.$el.scrollTop = 0;
}
I added a ref attribute called map to my Section tag and then at the end of my showUserLocationOnTheMap()
function I called this line (as you can also see above).
this.$refs.map.scrollToTop();
The problem is I'm getting this error:
Cannot read property 'scrollToTop' of undefined"