我不确定为什么我在循环图层中添加圆圈的for循环无效。
这是我的代码:
var linkDistance = $('#linkDistance').val();
var nodesCount = 8;
var bandwidth = "10 GB/s";
// gps coords for 505 Marquette
var rootLongitude = 35.088878;
var rootLatitude = -106.65262;
var mymap = L.map('mapid').setView([rootLongitude, rootLatitude], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
maxZoom: 18,
attribution: 'Map data © <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://mapbox.com">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(mymap);
function drawNext() {
var coords = [];
for (var i = 0; i <= nodesCount; i++) {
var radius = linkDistance*1000;
var angle = Math.PI*2*i/nodesCount;
var dx = radius*Math.cos(angle);
var dy = radius*Math.sin(angle);
coords.push([(rootLatitude + (180/Math.PI)*(dy/EARTH_RADIUS)), (rootLongitude + (180/Math.PI)*(dx/EARTH_RADIUS)/Math.cos(rootLatitude*Math.PI/180))]);
}
for (var i = 0; i < coords.length; i++) {
new L.Circle(coords[i], 200, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(mymap);
console.log("added circle to: " + coords[i]);
}
}
drawNext()
地图加载但没有圆圈出现。我可以毫不费力地制作一个圆圈,但似乎for循环让它破裂了。使用传单做这件事的正确方法是什么?谢谢!
答案 0 :(得分:2)
L.Circle(coords[i], 500), {...
linkDistance
的值,所选的初始缩放级别可能无法在视口中显示您的圈子。