多个圆圈上的传单地理围栏......检测内部/外部标记
Posted
技术标签:
【中文标题】多个圆圈上的传单地理围栏......检测内部/外部标记【英文标题】:Leaflet geofence on multiple circles ... detecting marker inside / outside 【发布时间】:2019-08-07 19:17:29 【问题描述】:我有一些代码可以检测市场何时被拖动到圆圈内外。
这很好用,但我只是想知道如何才能检测到一个市场和多个圈子,而不仅仅是一个。
这是当前代码:
var mymap = L.map('mapid',
center: [50.895763, -1.150556],
zoom: 16
);
L.tileLayer('https://api.tiles.mapbox.com/v4/id/z/x/y.png?access_token=pk.eyJ1Ijoic3RldmVuc2F0Y2giLCJhIjoiY2p5eDR6MWgzMHRvbjNocnJkN2d2MjRwaSJ9.wd0OtBUQQfUtNxdduQA3lg',
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox.streets'
).addTo(mymap);
var marker = new L.marker([50.896422, -1.148444],
draggable: true,
autoPan: true
).addTo(mymap);
var circle = L.circle([50.895763, -1.150556],
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5,
radius: 100
).addTo(mymap);
marker.on('drag', function(e)
var d = mymap.distance(e.latlng, circle.getLatLng());
var isInside = d < circle.getRadius();
circle.setStyle(
fillColor: isInside ? 'green' : '#f03'
)
);
但我想添加多个圆圈而不是一个。
我该怎么做?
【问题讨论】:
【参考方案1】:将圆圈放在一个数组中,然后用半径测试将 forEach()
放在它们上面 - 类似这样(替换您自己的设置圆圈的方法)
var mymap = L.map("mapid",
center: [50.895763, -1.150556],
zoom: 16
);
L.tileLayer(
"https://api.tiles.mapbox.com/v4/id/z/x/y.png?access_token=pk.eyJ1Ijoic3RldmVuc2F0Y2giLCJhIjoiY2p5eDR6MWgzMHRvbjNocnJkN2d2MjRwaSJ9.wd0OtBUQQfUtNxdduQA3lg",
maxZoom: 18,
attribution:
'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: "mapbox.streets"
).addTo(mymap);
var marker = new L.marker([50.896422, -1.148444],
draggable: true,
autoPan: true
).addTo(mymap);
var circles = [];
circles[0] = L.circle([50.895763, -1.150556],
color: "red",
fillColor: "#f03",
fillOpacity: 0.5,
radius: 100
).addTo(mymap);
circles[1] = L.circle([some - other - coordinates],
color: "red",
fillColor: "#f03",
fillOpacity: 0.5,
radius: 100
).addTo(mymap);
marker.on("drag", function(e)
circles.forEach(function(circle)
var d = mymap.distance(e.latlng, circle.getLatLng());
var isInside = d < circle.getRadius();
circle.setStyle(
fillColor: isInside ? "green" : "#f03"
);
);
);
【讨论】:
以上是关于多个圆圈上的传单地理围栏......检测内部/外部标记的主要内容,如果未能解决你的问题,请参考以下文章