Changing the marker icon on mouse hover
vanilla.html
react.html
vue.html
common.css
common.ts
variables.ts
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="../variables.ts"
></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {MARKER_PROPS_ACTIVE, MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, LOCATION} from '../variables';
window.map = null;
main();
async function main() {
// Waiting for all api elements to be loaded
await ymaps3.ready;
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapListener} = ymaps3;
const {YMapDefaultMarker} = await ymaps3.import('@yandex/ymaps3-default-ui-theme');
// Initialize the map
map = new YMap(
// Pass the link to the HTMLElement of the container
document.getElementById('app'),
// Pass the map initialization parameters
{location: LOCATION, showScaleInCopyrights: true},
[
// Add a map scheme layer
new YMapDefaultSchemeLayer({}),
// Add a layer of geo objects to display the markers
new YMapDefaultFeaturesLayer({})
]
);
const marker = new YMapDefaultMarker(MARKER_PROPS_DEFAULT);
const listener = new YMapListener({
layer: `${ymaps3.YMapDefaultFeaturesLayer.defaultProps.source}:markers`,
// Add a listener to the map and pass the handlers functions for the events you want to process
onMouseEnter: () => {
marker.update(MARKER_PROPS_HOVER);
},
onMouseLeave: () => {
marker.update(MARKER_PROPS_DEFAULT);
},
onClick: () => {
marker.update(MARKER_PROPS_ACTIVE);
}
});
const listenerMap = new YMapListener({
layer: 'any',
onClick: (object) => {
if (object?.type !== 'marker') marker.update(MARKER_PROPS_DEFAULT);
}
});
map.addChild(listener);
map.addChild(listenerMap);
map.addChild(marker);
}
</script>
<!-- prettier-ignore -->
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="./common.css" />
<link rel="stylesheet" href="../variables.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="../variables.ts"
></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
import {MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, MARKER_PROPS_ACTIVE, LOCATION} from '../variables';
import {DomEventHandler} from '@yandex/ymaps3-types';
window.map = null;
main();
async function main() {
// For each object in the JS API, there is a React counterpart
// To use the React version of the API, include the module @yandex/ymaps3-reactify
const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapListener} = reactify.module(ymaps3);
const {YMapDefaultMarker} = await reactify.module(await ymaps3.import('@yandex/ymaps3-default-ui-theme'));
const {useCallback, useMemo} = React;
function App() {
const [pointOnMarker, setPointOnMarker] = React.useState(false);
const [markerActive, setMarkerActive] = React.useState(false);
const onMouseEnter: DomEventHandler = useCallback(() => {
setPointOnMarker(true);
}, []);
const onMouseLeave: DomEventHandler = useCallback(() => {
setPointOnMarker(false);
}, []);
const onMouseClick: DomEventHandler = useCallback((object) => {
if (object && object.type === 'marker') {
setMarkerActive(true);
} else {
setMarkerActive(false);
}
}, []);
const markerProps = useMemo(
() => (markerActive ? MARKER_PROPS_ACTIVE : pointOnMarker ? MARKER_PROPS_HOVER : MARKER_PROPS_DEFAULT),
[markerActive, pointOnMarker]
);
return (
// Initialize the map and pass initialization parameters
<YMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
{/* Add a map scheme layer */}
<YMapDefaultSchemeLayer />
{/* Add a layer of geo objects to display the markers */}
<YMapDefaultFeaturesLayer />
<YMapDefaultMarker {...markerProps} />
<YMapListener
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onClick={onMouseClick}
layer={`${ymaps3.YMapDefaultFeaturesLayer.defaultProps.source}:markers`}
/>
<YMapListener onClick={onMouseClick} layer="any" />
</YMap>
);
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('app')
);
}
</script>
<!-- prettier-ignore -->
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="./common.css" />
<link rel="stylesheet" href="../variables.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="../variables.ts"
></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {MARKER_PROPS_ACTIVE, MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, LOCATION} from '../variables';
import {DomEventHandler} from '@yandex/ymaps3-types';
window.map = null;
async function main() {
const [ymaps3Vue] = await Promise.all([ymaps3.import('@yandex/ymaps3-vuefy'), ymaps3.ready]);
const vuefy = ymaps3Vue.vuefy.bindTo(Vue);
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapListener} = vuefy.module(ymaps3);
const {YMapDefaultMarker} = await vuefy.module(await ymaps3.import('@yandex/ymaps3-default-ui-theme'));
const App = Vue.createApp({
components: {
YMap,
YMapDefaultSchemeLayer,
YMapDefaultFeaturesLayer,
YMapListener,
YMapDefaultMarker
},
setup() {
const map = Vue.ref(null);
const pointOnMarker = Vue.ref(false);
const markerActive = Vue.ref(false);
const refMap = (ref) => {
window.map = ref?.entity;
};
const layerName = `${ymaps3.YMapDefaultFeaturesLayer.defaultProps.source}:markers`;
const onMouseEnter: DomEventHandler = () => {
pointOnMarker.value = true;
};
const onMouseLeave: DomEventHandler = () => {
pointOnMarker.value = false;
};
const onMouseClick: DomEventHandler = (object) => {
markerActive.value = object && object.type === 'marker';
};
return {
map,
LOCATION,
refMap,
pointOnMarker,
markerActive,
onMouseEnter,
onMouseLeave,
onMouseClick,
layerName
};
},
computed: {
markerProps() {
return this.markerActive
? MARKER_PROPS_ACTIVE
: this.pointOnMarker
? MARKER_PROPS_HOVER
: MARKER_PROPS_DEFAULT;
}
},
template: `
<YMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
<YMapDefaultSchemeLayer/>
<YMapDefaultFeaturesLayer/>
<YMapDefaultMarker
v-bind="markerProps"
/>
<YMapListener
:onMouseEnter="onMouseEnter"
:onMouseLeave="onMouseLeave"
:onClick="onMouseClick"
:layer="layerName"
/>
<YMapListener
:onClick="onMouseClick"
layer="any"
/>
</YMap>
`
});
App.mount('#app');
}
main();
</script>
<!-- prettier-ignore -->
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="./common.css" />
<link rel="stylesheet" href="../variables.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
.marker {
cursor: pointer;
width: 25px;
height: 25px;
border-radius: 50%;
position: relative;
transform: translate(-50%, -50%);
}
ymaps3.ready.then(() => {
ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', '@yandex/ymaps3-default-ui-theme@0.0');
});
import type {LngLat, YMapLocationRequest} from '@yandex/ymaps3-types';
import type {YMapDefaultMarkerProps} from '@yandex/ymaps3-default-ui-theme';
type MarkerProps = Omit<YMapDefaultMarkerProps, 'popup'>;
export const MARKER_PROPS_DEFAULT: MarkerProps = {
iconName: 'airport',
color: 'red',
size: 'small',
coordinates: [37.415, 55.9599]
};
export const MARKER_PROPS_HOVER: MarkerProps = {
iconName: 'airport',
color: 'darkblue',
size: 'small',
coordinates: [37.415, 55.9599]
};
export const MARKER_PROPS_ACTIVE: MarkerProps = {
iconName: 'airport',
size: 'normal',
color: 'red',
coordinates: [37.415, 55.9599]
};
export const LOCATION: YMapLocationRequest = {
center: [37.415, 55.9599], // starting position [lng, lat]
zoom: 10 // starting zoom
};