RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-522426

aliver's questions

Martin Hope
aliver
Asked: 2024-10-18 22:41:18 +0000 UTC

单击使用聚类的 Yandex Map v3 标记时如何运行函数?

  • 5

请告诉我。情况:有一张带点的地图。有一个 showInfo 函数,可以移动地图的“视线”、更改缩放以及信息块中活动点的信息。

我希望当点击某个点时触发这个函数。我无法想象该函数该放在哪里,因为我不理解(并且不再想理解)Yandex 地图 api 是如何工作的。

我只对善良的人抱有希望,因为我太累了,我的大脑已经停止运转了

initMap();

ymaps3.readyasync function initMap() { //当主 API 模块的所有组件加载完毕后,promise将被解析 wait ymaps3.ready;

const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapMarker, YMapControls, YMapLayer, YMapFeatureDataSource} = ymaps3;

const {YMapZoomControl} = await ymaps3.import('@yandex/[email protected]');

// Добавляем кластеризатор
const {YMapClusterer, clusterByGrid} = await ymaps3.import('@yandex/[email protected]');

// Иницилизируем карту
const map = new YMap(
    // Передаём ссылку на HTMLElement контейнера
    document.getElementById('map'),

    // Передаём параметры инициализации карты
    {
        location: {
            // Координаты центра карты
            center: [37.588144, 55.733842],

            // Уровень масштабирования
            zoom: 10
        }
    }
);

// Добавляем слой для отображения схематической карты
map.addChild(new YMapDefaultSchemeLayer({ customization: mapCustomizationJSON }));

// Добавляем к карте ресурс
map.addChild(new YMapFeatureDataSource({id:'my-source'}));
// Добавляем ресурс на карту
map.addChild(new YMapLayer({source: 'my-source', type: 'markers', zIndex:1800}));

// Пин
const contentPin = document.createElement('div');
contentPin.innerHTML = '<img style="width:20px;" src="https://www.freepnglogos.com/uploads/pin-png/pin-transparent-png-pictures-icons-and-png-backgrounds-36.png" />';

// Функция создания маркера (или че это)
function marker(feature) {
    const yMapMarker = new ymaps3.YMapMarker(
        {
            coordinates: feature.geometry.coordinates,
            source: 'my-source'
        },
        contentPin.cloneNode(true)
    );

    // yMapMarker.events.add('click', () => {
    //     showInfo(feature);
    // });

    return yMapMarker;
}

const cluster = (coordinates, features) =>
new ymaps3.YMapMarker(
    {
        coordinates,
        source: 'my-source'
    },
    circle(features.length).cloneNode(true)
);

function circle(count) {
    const circle = document.createElement('div');
    circle.classList.add('circle');
    circle.innerHTML = `
    <div class="circle-content"><span class="circle-text">${count}</span></div>`;
    return circle;
}

const points = pointsData.map((point, i) => ({
    type: 'Feature',
    id: i,
    geometry: {coordinates: [point.acf_fields.coordinates.y, point.acf_fields.coordinates.x]},
    properties: {
        name: point.acf_fields.point_name,
        // другие свойства
    }
}))

const clusterer = new YMapClusterer({
    method: clusterByGrid({gridSize: 64}),
    features: points,
    marker,
    cluster
});

map.addChild(clusterer);

//   Пытаюсь добавить кнопку Зума
map.addChild(
    // Using YMapControls you can change the position of the control
    new YMapControls({position: 'right'})
    // Add the zoom control to the map
    .addChild(new YMapZoomControl({}))
);

map.addChild(new YMapDefaultFeaturesLayer());

// Функция отображения информации
function showInfo(feature) {
    const infoElement = document.getElementById('point-info-container');
    
    // Вставляем в элемент inf-block данные, которые мы получим из переменной point
    document.getElementById('point-info__name').innerText = 'Info';
    document.getElementById('point-info__address').innerText = 'text';
    infoElement.style.display = 'block';

    map.update({
        location: {
            center: feature.geometry.coordinates,
            zoom: 14,
            duration: 1000,
        }
    });
    
}

Ps 据我了解,这可以通过标记(功能)来完成。我试着这样做

function marker(feature) {
        const yMapMarker = new ymaps3.YMapMarker(
            {
                coordinates: feature.geometry.coordinates,
                source: 'my-source'
            },
            contentPin.cloneNode(true)
        );

        yMapMarker.events.add('click', () => {
            showInfo(feature);
        });

        return yMapMarker;
    }

但我得到了一个错误。

test:55 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'add')
    at Object.marker (test:55:27)
    at c._getEntity (clusterer.js:1:1093)
    at clusterer.js:1:1228
    at Array.forEach (<anonymous>)
    at c._getVisibleEntities (clusterer.js:1:1202)
    at c._render (clusterer.js:1:1916)
    at c._onAttach (clusterer.js:1:2319)
    at c.AEC__implAttach (main.js:1:58810)
    at c.C57__implAttachTo (main.js:1:58391)
    at d._addDirectChild (main.js:1:61583)
yandex-maps-api
  • 1 个回答
  • 60 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5