Answer the question
In order to leave comments, you need to log in
How to get yandex marker in vue-yandex-maps?
I ran into such a problem, I use the vue-yandex-maps
component to work with the yandex map.
This is how the output of the map and its labels looks like:
<template>
<ul>
<li
v-for="object in objects"
:key="object.id"
@mouseleave="onMouseleaveObject"
@mouseenter="onMouseenterObject"
>
{{ object.title }}
</li>
</ul>
<yandex-map
ref="map"
:settings="map.settings"
:controls="map.controls"
:coords="mapCoords"
:show-all-markers="true"
>
<ymap-marker
v-for="marker in markers"
:key="marker.id"
:marker-id="marker.id"
:coords="marker.coords"
:hint-content="marker.title"
:icon="map.markerIcon"
@mouseenter="onMouseenterMarker"
@mouseleave="onMouseleaveMarker"
/>
</yandex-map>
</template>
<script>
import { yandexMap, ymapMarker } from 'vue-yandex-maps';
const MAP_MARKER_DEFAULT = require('@/assets/images/map-markers/marker-circle.svg');
const MAP_MARKER_ACTIVE = require('@/assets/images/map-markers/marker-circle-active.svg');
export default {
components: {
yandexMap,
ymapMarker
},
data: () => ({
map: {
settings: {
apiKey: 'тут ключ',
lang: 'ru_RU',
coordorder: 'latlong',
version: '2.1'
},
controls: [],
markerIcon: {
layout: 'default#image',
imageHref: MAP_MARKER_DEFAULT,
imageSize: [48, 48],
imageOffset: [-24, -48]
}
}
},
methods: {
onMouseenterObject (id) {
console.log('onMouseenterObject');
},
onMouseleaveObject () {
console.log('onMouseleaveObject');
},
onMouseenterMarker (event) {
const marker = event.get('target');
this.setMarkerIcon(marker, MAP_MARKER_ACTIVE);
},
onMouseleaveMarker (event) {
const marker = event.get('target');
this.setMarkerIcon(marker, MAP_MARKER_DEFAULT);
},
setMarkerIcon (marker, icon) {
marker.options.set('iconImageHref', icon);
}
}
};
</script>
onMouseleaveObject (index) { // В шаблоне я передавал index
const marker = this.$refs.map.mayMap.geoObjects.get(index);
console.log(marker); // undefined
this.setMarkerIcon(marker, MAP_MARKER_ACTIVE);
}
Answer the question
In order to leave comments, you need to log in
No need to manipulate markers directly. Store the index of the active marker in the component data, set its value on enter/leave events, depending on which you can pass icon images to the marker component instances:
data: () => ({
map: {
activeMarkerIndex: null,
markerIconImages: [ MAP_MARKER_DEFAULT, MAP_MARKER_ACTIVE ],
...
<ymap-marker
v-for="(n, i) in markers"
:icon="{
...map.markerIcon,
imageHref: map.markerIconImages[+(map.activeMarkerIndex === i)],
}"
@mouseenter="map.activeMarkerIndex = i"
@mouseleave="map.activeMarkerIndex = null"
...
<li
v-for="(n, i) in objects"
@mouseenter="map.activeMarkerIndex = i"
@mouseleave="map.activeMarkerIndex = null"
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question