Javascript 查找最近的点,并将最近点的特性显示在传单中用户创建的标记中
我正试图通过传单在特定位置的地图上查找最近的标记,希望显示用户创建的标记上的弹出信息,即查找最近点,弹出信息应包含geojson数据层的最近点属性。 示例maker.bindpopup(feature.properties.name).addTo(map)Javascript 查找最近的点,并将最近点的特性显示在传单中用户创建的标记中,javascript,leaflet,Javascript,Leaflet,我正试图通过传单在特定位置的地图上查找最近的标记,希望显示用户创建的标记上的弹出信息,即查找最近点,弹出信息应包含geojson数据层的最近点属性。 示例maker.bindpopup(feature.properties.name).addTo(map) (函数(){ var geojosn1='url1'; var geojsonn2='url2'; var站, $body=$('body'), $locate=$(“#locate”), $findNearest=$(“#查找最近的”),
(函数(){
var geojosn1='url1';
var geojsonn2='url2';
var站,
$body=$('body'),
$locate=$(“#locate”),
$findNearest=$(“#查找最近的”),
$status=$(“#status”);
$.getJSON(geojosn1,函数(数据){
//$(“#加载程序”).fadeOut();
$body.addClass('loaded');
stations=L.geoJson(数据{
pointToLayer:功能(特性、latlng){
返回L.circleMarker(车床{
笔画:错,
fillColor:'橙色',
不透明度:1,
半径:2
});
}
}).addTo(地图);
$locate.fadeIn().on('click',函数(e){
$status.html('finding your location');
如果(!navigator.geolocation){
警报(对不起,您的浏览器不支持地理定位);
返回;
}
$body.removeClass('loaded');
navigator.geolocation.getCurrentPosition(成功,错误);
$locate.fadeOut();
});
});
功能成功(职位){
$body.addClass('loaded');
var currentPos=[位置.坐标.纬度,位置.坐标.经度];
map.setView(currentPos,zoomLevel);
var myLocation=L.标记(当前位置)
.addTo(地图)
.bindTooltip(“您在这里”)
.openTooltip();
$findNearest.fadeIn()
.on('click',函数(e){
$findNearest.fadeOut();
$status.html('查找最近的位置')
查询功能(当前位置,5);
myLocation.unbindTooltip();
});
};
函数查询功能(currentPos、numResults){
var距离=[];
站。每层(功能(l){
var distance=L.latLng(当前位置).distanceTo(L.getLatLng())/1000;
距离。推(距离);
});
距离.排序(函数(a,b){
返回a-b;
});
var stationsLayer=L.featureGroup();
站。每层(功能(l){
var distance=L.latLng(当前位置).distanceTo(L.getLatLng())/1000;
if(距离<距离[numResults]){
l、 bindTooltip(distance.toLocalString()+“距离当前位置公里”);
L.多段线([currentPos,L.GetLatling()]{
颜色:“橙色”,
体重:2,
不透明度:1,
dashArray:“5,10”
}).addTo(stationsLayer);
}
});
map.flytobunds(stationsLayer.getBounds(),{duration:3,easelinear:.1});
map.on('zoomend',function(){
map.addLayer(StationLayer);
})
}
})()
试试这个:
function queryFeatures(currentPos, numResults) {
var stationsLayer = L.featureGroup();
var result = {
layer: null,
dis: 0,
marker: null
};
stations.eachLayer(function(l) {
var distance = L.latLng(currentPos).distanceTo(l.getLatLng())/1000;
if(result.layer == null || distance < result.dis) {
var line = L.polyline([currentPos, l.getLatLng()], {
color : 'orange',
weight : 2,
opacity: 1,
dashArray : "5, 10"
});
result = {
layer: line,
dis: distance,
marker: l
};
}
});
result.marker.bindTooltip(result.dis.toLocaleString() + ' km from current location.');
result.layer.addTo(stationsLayer);
map.flyToBounds(stationsLayer.getBounds(), {duration : 3, easeLinearity: .1 });
map.on('zoomend', function() {
map.addLayer(stationsLayer);
});
}
函数查询功能(currentPos、numResults){
var stationsLayer=L.featureGroup();
var结果={
图层:空,
dis:0,
标记:空
};
站。每层(功能(l){
var distance=L.latLng(当前位置).distanceTo(L.getLatLng())/1000;
if(result.layer==null | | distance
和marker应该是show properties of point data您可以通过以下方法实现:result.marker.feature.properties
如果我有两个geojson层,我想在标记上显示geojson多边形层的属性。你必须找到多边形,然后用polygon.feature.properties
读取它,然后你可以打开一个包含数据的弹出窗口。请接受答案,如果它有工作(与第一个问题)弹出窗口应该只有一个两层
function queryFeatures(currentPos, numResults) {
var stationsLayer = L.featureGroup();
var result = {
layer: null,
dis: 0,
marker: null
};
stations.eachLayer(function(l) {
var distance = L.latLng(currentPos).distanceTo(l.getLatLng())/1000;
if(result.layer == null || distance < result.dis) {
var line = L.polyline([currentPos, l.getLatLng()], {
color : 'orange',
weight : 2,
opacity: 1,
dashArray : "5, 10"
});
result = {
layer: line,
dis: distance,
marker: l
};
}
});
result.marker.bindTooltip(result.dis.toLocaleString() + ' km from current location.');
result.layer.addTo(stationsLayer);
map.flyToBounds(stationsLayer.getBounds(), {duration : 3, easeLinearity: .1 });
map.on('zoomend', function() {
map.addLayer(stationsLayer);
});
}