首页 > 解决方案 > 如何在 Openlayer 5 和 vanilla js 中使用 MousePosition?

问题描述

我的代码有什么问题?我想得到坐标mousePosition。我使用openlayer v5。

var map = new ol.Map({
    target: 'map2',
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        })
    ],
    view: new ol.View({
        center: ol.proj.fromLonLat([116.041389, 3.073093]),
        zoom: 7.3
    })
});
var mousePositionControl = new ol.MousePosition({
    coordinateFormat: createStringXY(2),
    projection: 'EPSG:4326',
    className: 'custom-mouse-position',
    target: document.getElementById('mouse-position'),
    undefinedHTML: ' '
});

执行上面的代码会得到一个

“ReferenceError:未定义 createStringXY”错误。

标签: javascriptopenlayers-5

解决方案


推荐阅读