在传单中绘制折线?


24

这是我在Leaflet中使用的编码:

    var pointA = new L.LatLng(28.635308, 77.22496);
    var pointB = new L.LatLng(28.984461, 77.70641);
    var pointList = [pointA, pointB];

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

    });

    map.addLayer(firstpolyline);

该地图未显示任何结果。我只需要从一个点到另一个点的直线,只是将其可视化即可,而不是大地测量的正确性。点(在传单“圆圈”中)在地图上完美显示。

Answers:


36

您的代码是否直接粘贴?如果是这样的话,

var pointA = new L.LatLng(28.635308, 77.22496);
var pointB = new L.LatLng(28.984461, 77.70641);
var pointList = [pointA, pointB];

var firstpolyline = new L.polyline(pointList {
color: 'red',
weight: 3,
opacity: 0.5
smoothFactor: 1

});

第5和8行缺少逗号,第12行使用firstpolyline.addTo(map)。做了

var pointA = new L.LatLng(28.635308, 77.22496);
var pointB = new L.LatLng(28.984461, 77.70641);
var pointList = [pointA, pointB];

var firstpolyline = new L.Polyline(pointList, {
    color: 'red',
    weight: 3,
    opacity: 0.5,
    smoothFactor: 1
});
firstpolyline.addTo(map);

我只能看到一个错误,并且已解决。但是折线仍然没有显示在我的地图上。我究竟做错了什么?
逆流

您只是错过了最后一小段代码。折线已正确编码,但并未按照Lealflet的要求将其添加到地图中:firstpolyline.addTo(map);


1

在中使用小写的“折线” L.polyline(...)。这将起作用。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.