当前位置: 首页 > 知识库问答 >
问题:

在传单中选择两个标记并在它们之间画线

向杜吟
2023-03-14

我对传单很陌生。

我在传单上的地图上画了多个标记/圆圈标记。

var map = L.map('map').setView([51.49521, -0.10062], 13);
L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
    }).addTo(map);

 // get all 6 points
  var points = new Array(
[51.49346, -0.11518],
[51.49827, -0.06763],
[51.48331, -0.08154],
[51.52284, -0.09974],
[51.51932, -0.06695],
[51.50949, -0.1363]
   );

  // centerpoint
  var centerPoint = new L.LatLng(51.49521, -0.10062);
  var marker1 = L.marker([51.49521, -0.10062]).addTo(map);


  // adding allo points to map
  for (var i =0 ; i < points.length; i++)
  { 
     // here I can use marker also(if solution is possible with markers)
L.circleMarker([points[i][0],points[i][1]]).addTo(map);
var point = new L.LatLng(points[i][0],points[i][1]);
var pointList = [point, centerPoint];

    var firstpolyline = new L.Polyline(pointList, {
    color: 'red',
    weight: 5,
    smoothFactor: 1

   }).addTo(map);
  }

共有1个答案

穆乐逸
2023-03-14

您必须存储所选内容(但是它可以是polylinepoints或标记)。当选择两个或多个标记时,必须在polyline中添加点--它在地图上绘制线,否则必须从polyline中删除点。有关polyline的详细信息:http://leafletjs.com/reference.html#polyline。

请参见下面的代码,例如:

// Init map
var map = L.map('map').setView([53.902257, 27.561640], 13);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

// Init selection and lines logic
var selection = [];
var polyline = L.polyline([], {color: 'red'}).addTo(map);

var onClick = function () {
    var index = selection.indexOf(this);
    if (index !== -1) {
        this.setRadius(10);
        selection.splice(index, 1);
        polyline.spliceLatLngs(index, 1);
    } else {
        this.setRadius(25);
        selection.push(this);
        polyline.addLatLng(this.getLatLng())
    }
};

// Init circle markers
L.circleMarker([53.90, 27.56]).on('click', onClick).addTo(map);
L.circleMarker([53.92, 27.60]).on('click', onClick).addTo(map);
L.circleMarker([53.88, 27.60]).on('click', onClick).addTo(map);

// Init selection droping on ESC pressed
L.DomEvent.on(document, 'keydown', function (e) {
    if (e.keyCode === 27) {
        var oldSelection = selection.slice(0);
        for (var i = 0, l = oldSelection.length; i < l; i++) {
            oldSelection[i].fire('click');
        }
    }
});

UPD:

类似地,请参见更新后的代码:

var map = L.map('map').setView([51.49521, -0.10062], 13);
L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
}).addTo(map);

// get all 6 points
var points = [
    [51.49346, -0.11518],
    [51.49827, -0.06763],
    [51.48331, -0.08154],
    [51.52284, -0.09974],
    [51.51932, -0.06695],
    [51.50949, -0.1363]
];

// polyline
var selection = [];
var polyline = new L.Polyline([], {
    color: 'red',
    weight: 5,
    smoothFactor: 1
}).addTo(map);

var changeMarkerState = function (marker, select) {
    if (marker instanceof L.CircleMarker) {
        if (select) {
            marker.setRadius(25);
        } else {
            marker.setRadius(10);
        }
    }
    if (marker instanceof L.Marker) {
        if (select) {
            marker.options.title = 'selected';
        } else {
            marker.options.title = 'unselected';
        }
        marker.setIcon(new L.Icon.Default());
    }
};

var onClick = function () {
    var index = selection.indexOf(this);
    if (index !== -1) {
        changeMarkerState(this, false);
        selection.splice(index, 1);
        polyline.spliceLatLngs(index, 1);
    } else {
        changeMarkerState(this, true);
        selection.push(this);
        polyline.addLatLng(this.getLatLng())
    }
};

// centerpoint
var centerPoint = new L.LatLng(51.49521, -0.10062);
var marker1 = L.marker([51.49521, -0.10062],
        {title: 'unselected'}).on('click', onClick).addTo(map);


// adding allo points to map
for (var i = 0, l = points.length; i < l; i++)
{
    // here I can use marker also(if solution is possible with markers)
    L.circleMarker(points[i]).on('click', onClick).addTo(map);
}
 类似资料:
  • 我试图在传单中的标记(从JSON数据生成)之间插入行。我看到了一个示例,但它不能使用JSON数据。我能看到标记,但没有线条出现。 我的JSON数据:

  • 问题内容: 我有一个存储表,,,,如: 现在,如果我有IP地址,如何检索匹配记录? 编辑 根据戈登的答案(我遇到编译错误),这就是我的想法: 但这给了我一个错误: 有任何想法吗? 问题答案: 痛苦的。SQL Server具有糟糕的字符串操作功能。但是,它提供了。此方法将IP地址转换为较大的十进制值以进行比较: 我应该注意,IP地址通常以4字节无符号整数的形式存储在数据库中。这使比较容易得多。。。尽

  • 问题内容: 我正在使用数据库存储日志,其中“日期”列保存了插入日期。日期的格式为“ MM / DD / YY”。请任何人建议我如何在两个特定日期之间选择数据。例如,我尝试了这个: 但是我想这行不通,因为日期不是数字。谢谢您的帮助!:) 问题答案: 使用关键字:

  • 在像这样的网站上http://wikitravel.org/en/San_Francisco,诸如“Districts”、“Understand”、“Get in”等部分实际上并不包含HTML中的整个部分。节实际上只是标题中的跨类。正因为如此,我们不能简单地通过选择id来获取wiki文档的某些部分。 但是,是否可以收集两个标记之间的所有html?比如说我想要“四处走动”部分。我该如何发出一个选择器

  • 问题内容: 我正在用Matplotlib绘制两个子图,基本上如下: 我可以在这两个子图之间画线吗?我该怎么办? 问题答案: 在许多情况下,其他答案的解决方案不是最优的(因为它们只有在计算出点数后未对图进行任何更改的情况下才会起作用)。 更好的解决方案将使用特别设计的:

  • 问题内容: 我正在尝试在2列之间选择一个值。这是我的数据集 我的目标是(如果我的值为2)是选择 ID为1 (在from和to之间)的行。所以这是我正在使用的查询: 这是MySQL执行此查询时返回的结果: 我正在寻找的结果如下: 我尝试使用<和>等。但是,我总是得到两个结果。任何帮助将非常感激。 问题答案: 所以,您不希望下限具有包容性,对吗?