使用JavaScript标记连接地图中的点

使用JavaScript标记连接地图中的点,javascript,marker,Javascript,Marker,因此,我尝试为我的一个项目制作一个交互式地图 以下是我所做的: 我想知道这两个点是如何用一条线连接起来的 注意:这不是谷歌地图API 代码如下: <script> $(function() { $("#world_map").vectorMap({ map: "world_mill", normalizeFunction: "polynomial",

因此,我尝试为我的一个项目制作一个交互式地图

以下是我所做的:

我想知道这两个点是如何用一条线连接起来的

注意:这不是谷歌地图API

代码如下:

<script>
    $(function() {
        $("#world_map").vectorMap({
            map: "world_mill",
            normalizeFunction: "polynomial",
            hoverOpacity: .7,
            hoverColor: false,
            regionStyle: {
                initial: {
                    fill: "#e3eaef"
                }
            },
            markerStyle: {
                initial: {
                    "r": 9,
                    "fill": window.theme.primary,
                    "fill-opacity": .95,
                    "stroke": "#fff",
                    "stroke-width": 7,
                    "stroke-opacity": .4
                },
                hover: {
                    "stroke": "#fff",
                    "fill-opacity": 1,
                    "stroke-width": 1.5
                }
            },
            backgroundColor: "transparent",
            zoomOnScroll: false,
            markers: [{
                    latLng: [49.009724, 2.547778],
                    name: "Paris"
                },
                {
                    latLng: [37.983810, 23.727539],
                    name: "Athens"
                }
            ]
        });
        setTimeout(function() {
            $(window).trigger('resize');
        }, 250)
    });
</script>

$(函数(){
$(“世界地图”).vectorMap({
地图:“世界磨坊”,
正规化函数:“多项式”,
不透明度:.7,
hoverColor:false,
地区风格:{
首字母:{
填写:“e3eaef”
}
},
markerStyle:{
首字母:{
“r”:9,
“填充”:window.theme.primary,
“填充不透明度”:.95,
“笔划”:“fff”,
“笔划宽度”:7,
“笔划不透明度”:
},
悬停:{
“笔划”:“fff”,
“填充不透明度”:1,
“笔划宽度”:1.5
}
},
背景色:“透明”,
错,
标记:[{
latLng:[49.009724,2.547778],
名称:“巴黎”
},
{
拉丁语:[37.983810,23.727539],
名称:“雅典”
}
]
});
setTimeout(函数(){
$(window.trigger('resize');
}, 250)
});

我无法测试此功能,但请告诉我它是否有效。其思想是将
标记
数组中标记的索引传递给此函数

此外,您还需要添加一个
元素来覆盖地图

<canvas id="canvas" height="400" width="500"></canvas>


 function drawLine(fromIndex, toIndex) {
        const canvas = document.querySelector('#canvas');

        var fromMarker = $('circle [data-index="'+fromIndex+'"]').position();
        var toMarker = $('circle [data-index="'+toIndex+'"]').position();

        if (!canvas.getContext) {
            return;
        }
        const ctx = canvas.getContext('2d');

        // set line stroke and line width
        ctx.strokeStyle = 'red';
        ctx.lineWidth = 1;

        // draw a red line
        ctx.beginPath();
        ctx.moveTo(fromMarker.x, fromMarker.y);
        ctx.lineTo(toMarker.x, toMarker.y);
        ctx.stroke();

    }
// know the indexes of the 2 markers in order of the way they're listed
drawLine(0, 1);

功能绘制线(从索引到索引){
const canvas=document.querySelector(“#canvas”);
var fromMarker=$('circle[data index=“”+fromIndex+“]”)位置();
var-toMarker=$('circle[data index=“”+toIndex+“]”)位置();
如果(!canvas.getContext){
返回;
}
const ctx=canvas.getContext('2d');
//设置线条笔划和线条宽度
ctx.strokeStyle=‘红色’;
ctx.lineWidth=1;
//划红线
ctx.beginPath();
ctx.moveTo(fromMarker.x,fromMarker.y);
ctx.lineTo(toMarker.x,toMarker.y);
ctx.stroke();
}
//了解这两个标记的索引,按其列出的顺序排列
抽绳(0,1);

感谢您的回复。不幸的是,当我实现你的代码时,“如果”条件被激活,在没有连续性的情况下退出函数。出于某种原因,它说getContext不是definedOops,我忽略了在页面上需要实际的canvas元素。答案已更新。不确定是否可以在地图上覆盖画布。。。