'use script' //开发环境建议开启严格模式
;(function (window, mars3d) {
//创建widget类,需要继承BaseWidget
class MyWidget extends mars3d.widget.BaseWidget {
//外部资源配置
get resources() {
return ['view.css']
}
//弹窗配置
get view() {
return {
type: 'append',
url: 'view.html',
parent: 'body',
}
}
//初始化[仅执行1次]
create() {
this.storageName = 'mars3d_queryBaiduPOI'
this.pageSize = 6
this.arrdata = []
this.counts = 0
this.allpage = 0
this.thispage = 0
//创建矢量数据图层
this.graphicLayer = new mars3d.layer.GraphicLayer({
name: this.config.name,
pid: 99, //图层管理 中使用,父节点id
})
//鼠标单击后的信息面板弹窗
this.graphicLayer.bindPopup(
function (event) {
let item = event.graphic?.attr
if (!item) {
return
}
var name
if (item.detail_info && item.detail_info.detail_url) {
name = '' + item.name + ''
} else {
name = item.name
}
var inHtml = '
'
return inHtml
},
{
anchor: [0, -10],
}
)
//查询控制器
this.baiduPOI = new mars3d.query.BaiduPOI()
}
//每个窗口创建完成后调用
winCreateOK(opt, result) {
if (opt.type != 'append') {
return
}
var that = this
var img = $('#map-querybar img')
img.each((index, item) => {
$(item).attr('src', this.path + $(item).attr('src'))
})
if (this.config.position) {
$('#map-querybar').css(this.config.position)
}
if (this.config.style) {
$('#map-querybar').css(this.config.style)
}
// 搜索框
$('#txt_querypoi').click(function () {
// 文本框内容为空
if ($.trim($(this).val()).length == 0) {
that.hideAllQueryBarView()
// that.showHistoryList() // 显示历史记录
}
})
var timetik = 0
// 搜索框绑定文本框值发生变化,隐藏默认搜索信息栏,显示匹配结果列表
$('#txt_querypoi').bind('input propertychange', () => {
clearTimeout(timetik)
timetik = setTimeout(() => {
this.hideAllQueryBarView()
this.clearLayers()
var queryVal = $.trim($('#txt_querypoi').val())
if (queryVal.length == 0) {
// 文本框内容为空,显示历史记录
// this.showHistoryList()
} else {
this.autoTipList(queryVal, true)
}
}, 500)
})
// 点击搜索查询按钮
$('#btn_querypoi').click(() => {
clearTimeout(timetik)
this.hideAllQueryBarView()
var queryVal = $.trim($('#txt_querypoi').val())
this.strartQueryPOI(queryVal, true)
})
//绑定回车键
$('#txt_querypoi').bind('keydown', (event) => {
if (event.keyCode == '13') {
$('#btn_querypoi').click()
}
})
// 返回查询结果面板界面
$('#querybar_detail_back').click(() => {
this.hideAllQueryBarView()
$('#querybar_resultlist_view').show()
})
}
//打开激活
activate() {
this.map.addLayer(this.graphicLayer)
//单击地图事件
this.map.on(mars3d.EventType.clickMap, this.onMapClick, this)
}
//关闭释放
disable() {
this.map.removeLayer(this.graphicLayer)
//释放单击地图事件
this.map.off(mars3d.EventType.clickMap, this.onMapClick, this)
this.hideAllQueryBarView()
this.clearLayers()
}
onMapClick(event) {
// 点击地图区域,隐藏所有弹出框
if ($.trim($('#txt_querypoi').val()).length == 0) {
this.hideAllQueryBarView()
$('#txt_querypoi').blur()
}
}
hideAllQueryBarView() {
$('#querybar_histroy_view').hide()
$('#querybar_autotip_view').hide()
$('#querybar_detail_view').hide()
$('#querybar_resultlist_view').hide()
}
// 点击面板条目,自动填充搜索框,并展示搜索结果面板
autoSearch(name) {
$('#txt_querypoi').val(name)
$('#btn_querypoi').trigger('click')
}
//===================与后台交互========================
//显示智能提示搜索结果
autoTipList(text, queryEx) {
//输入经纬度数字时
if (this.isLonLat(text)) {
return
}
//查询外部widget
if (this.hasExWidget() && queryEx) {
this.autoExTipList(text)
return
}
//查询外部widget
this.baiduPOI.autoTip({
text: text,
location: this.map.getCenter(),
success: (result) => {
var inhtml = ''
var pois = result.list
for (var index = 0; index < pois.length; index++) {
var name = pois[index].name
// var num = pois[index].num;
// if (num > 0) continue;
inhtml += "没有找到"' + this.queryText + '"相关结果
'
} else {
this.objResultData = this.objResultData || {}
for (var index = 0; index < this.arrdata.length; index++) {
var item = this.arrdata[index]
var startIdx = (this.thispage - 1) * this.pageSize
item.index = startIdx + (index + 1)
var _id = index
var _mc
if (item.detail_info && item.detail_info.detail_url) {
_mc = '