import { yandexMap, ymapMarker, loadYmap } from 'vue-yandex-maps';
components: {
yandexMap,
ymapMarker,
},
data: () => ({
coords: null,
markers: [],
settings: { /* ... */ },
}),
methods: {
onClick(e) {
this.markers.push({
id: 1 + Math.max(0, ...this.markers.map(n => n.id)),
coords: e.get('coords'),
});
},
},
async mounted() {
await loadYmap({ ...this.settings, debug: true });
ymaps.geolocation.get().then(res => {
this.coords = res.geoObjects.position;
});
},
<yandex-map
v-if="coords"
:coords="coords"
@click="onClick"
>
<ymap-marker
v-for="n in markers"
:key="n.id"
:marker-id="n.id"
:coords="n.coords"
></ymap-marker>
</yandex-map>
<Placemark
options={{
iconLayout: 'default#image',
iconImageHref: 'здесь путь к вашей картинке',
}}
/>
map.events.add('boundschange', function() {
map.balloon.setPosition(map.getCenter());
});
map.events.add('actiontickcomplete', function(e) {
const { globalPixelCenter, zoom } = e.get('tick');
map.balloon.setPosition(map.options.get('projection').fromGlobalPixels(globalPixelCenter, zoom));
});
const onPlacemarkClick = e => {
const placemarkData = e.get('target').properties.get('placemarkData');
...
};
<Placemark
onClick={onPlacemarkClick}
properties={{
placemarkData: {
city: point.City,
address: point.Address,
...
},
...
}}
...
/>
map.panTo(...).then(() => map.setZoom(...))
let
map = null,
marker = null;
document.addEventListener('click', function(e) {
if (!e.target.classList.contains('contacts__address--link')) {
return;
}
e.preventDefault();
const
addressData = e.target.closest('.js-address').dataset,
coord = [ addressData.longtitude, addressData.latitude ];
if (!map) {
map = new ymaps.Map('map', {
center: coord,
zoom: 17,
});
marker = new ymaps.Placemark(coord);
map.geoObjects.add(marker);
map.behaviors.disable('scrollZoom');
} else {
map.setCenter(coord);
marker.geometry.setCoordinates(coord);
}
});
width: 100% !important;
, а родительский элемент canvas'а карты имеет нулевые размеры.new ymaps.Map("map", {
кастомные свойства для иконки прописал...
<div id="buttons"></div>
<div id="map"></div>
const items = [
{ coord: [ 55.751244, 37.618423 ], title: 'Moscow' },
{ coord: [ 48.864716, 2.349014 ], title: 'Paris' },
{ coord: [ 34.052235, -118.243683 ], title: 'Los Angeles' },
];
ymaps.ready(function () {
const map = new ymaps.Map('map', {
zoom: 9,
center: items[Math.random() * items.length | 0].coord,
});
items.forEach(n => map.geoObjects.add(new ymaps.Placemark(n.coord)));
const buttons = document.querySelector('#buttons');
buttons.innerHTML = items.map((n, i) =>
`<button data-index="${i}">${n.title}</button>`
).join('');
buttons.addEventListener('click', ({ target: { dataset: { index } } }) => {
if (index) {
map.setCenter(items[index].coord);
}
});
});
<option value="[55.751244,37.618423]">Москва</option>
select.addEventListener('change', function() {
map.setCenter(JSON.parse(this.value));
});