问题描述
我正在尝试创建一个搜索栏,该标记将标记添加到传单地图中要搜索的位置。我想要的是:
- 当用户搜索某个位置并按下搜索按钮时,该位置的坐标上应出现一个标记。
- 这些位置不是通用的。这意味着它们不是像加拿大,北京等受欢迎的地区。它们是我在我的大学中使用typeahead.js手动添加到搜索栏中的位置。代码如下:
var substringMatcher = function(strs) {
return function findMatches(q,cb) {
var matches,substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q,'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`,add it to the `matches` array
$.each(strs,function(i,str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var places = ['Dean Office','HOD Space Sciences','HOD Mechanical Engineering','HOD Electrical Engineering','HOD Materials Science and Engineering','HOD Maths','HOD Aerospace Engineering','Vice Chancellor Office','Department of Avionics and Aerospace','Department of Electrical Engineering','Department of Materials Science and Engineering','Transport Office','Student Affairs'
];
$('#the-basics .typeahead').typeahead({
hint: true,highlight: true,minLength: 1
},{
name: 'places',source: substringMatcher(places)
});
我不知道如何使用预先选择的选项将标记添加到地图,并且对JS函数和事件不是很好。有人可以在用户在搜索栏中选择他/她的选项并单击搜索按钮之后,建议如何将标记添加到地图上吗?
P.S。我试过了,但是没用:
$('#the-basics .typeahead').on('typeahead:selected',function(evt,item) {
var pulsingIcon = L.icon.pulse({iconSize:[10,10],color:'red'});
var search = L.marker([33.521500,73.176870],{icon: pulsingIcon});
if (places == 'Dean Office') {
search.setLatLng([33.521500,73.176870])
}
else (places == 'HOD Space Sciences') {
search.setLatLng([33.519560,73.176355])
}
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)