"use script" //开发环境建议开启严格模式
;(function (window, mars3d) {
//创建widget类,需要继承BaseWidget
class MyWidget extends es5widget.BaseWidget {
//外部资源配置
get resources() {
return ["view.css"]
}
//弹窗配置
get view() {
return {
type: "append",
url: "view.html",
parent: "body"
}
}
//初始化[仅执行1次]
create() {
this.storageName = "mars3d_queryGaodePOI"
this.pageSize = 6
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
}
let inHtml = `
"
return inHtml
},
{ has3dtiles: false }
)
//查询控制器
this._queryPoi = new mars3d.query.GaodePOI({
// city: '合肥市',
})
}
//每个窗口创建完成后调用
winCreateOK(opt, result) {
if (opt.type != "append") {
return
}
let that = this
let 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() // 显示历史记录
}
})
let timetik = 0
// 搜索框绑定文本框值发生变化,隐藏默认搜索信息栏,显示匹配结果列表
$("#txt_querypoi").bind("input propertychange", () => {
clearTimeout(timetik)
timetik = setTimeout(() => {
this.hideAllQueryBarView()
this.clearLayers()
let queryVal = $.trim($("#txt_querypoi").val())
if (queryVal.length == 0) {
// 文本框内容为空,显示历史记录
this.showHistoryList()
} else {
this.autoTipList(queryVal, true)
}
}, 500)
})
// 点击搜索查询按钮
$("#btn_querypoi").click(() => {
clearTimeout(timetik)
this.hideAllQueryBarView()
let queryVal = $.trim($("#txt_querypoi").val())
this.strartQueryPOI(queryVal, true)
// //演示:抛出事件
// let layer = this.map.getLayer(203012, "id"); //文庙图层
// layer.show = true; //为了演示勾选
// this.map.addLayer(layer);
// es5widget.fire("checkLayer", { layer });
})
//绑定回车键
$("#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)
// 下侧状态栏提示
const locationBar = this.map.controls.locationBar?.container
if (locationBar) {
this.queryAddressDOM = mars3d.DomUtil.create(
"div",
"mars3d-locationbar-content mars3d-locationbar-autohide",
this.map.controls.locationBar.container
)
this.queryAddressDOM.style.marginRight = "50px"
}
//单击地图事件
this.map.on(mars3d.EventType.clickMap, this.onMapClick, this)
this.map.on(mars3d.EventType.cameraChanged, this.onMapCameraChanged, this)
this.onMapCameraChanged()
}
//关闭释放
disable() {
this.map.removeLayer(this.graphicLayer)
//释放单击地图事件
this.map.off(mars3d.EventType.clickMap, this.onMapClick, this)
this.map.off(mars3d.EventType.cameraChanged, this.onMapCameraChanged, this)
if (this.queryAddressDOM) {
mars3d.DomUtil.remove(this.queryAddressDOM)
delete this.queryAddressDOM
}
this.hideAllQueryBarView()
this.clearLayers()
}
onMapClick(event) {
// 点击地图区域,隐藏所有弹出框
if ($.trim($("#txt_querypoi").val()).length == 0) {
this.hideAllQueryBarView()
$("#txt_querypoi").blur()
}
}
onMapCameraChanged(event) {
let radius = this.map.camera.positionCartographic.height //单位:米
if (radius > 100000) {
this.address = null
this.queryAddressDOM.innerHTML = ""
return
}
this._queryPoi.getAddress({
location: this.map.getCenter(),
success: (result) => {
// console.log("地址", result);
this.address = result
this.queryAddressDOM.innerHTML = "地址:" + result.address
}
})
}
hideAllQueryBarView() {
$("#querybar_histroy_view").hide()
$("#querybar_autotip_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._queryPoi.autoTip({
text: text,
city: this.address?.city,
location: this.map.getCenter(),
success: (result) => {
let inhtml = ""
let pois = result.list
for (let index = 0; index < pois.length; index++) {
let name = pois[index].name
// var num = pois[index].num;
// if (num > 0) continue;
inhtml += "没有找到"' + this.queryText + '"相关结果
'
} else {
this.objResultData = this.objResultData || {}
for (let index = 0; index < data.length; index++) {
let item = data[index]
let startIdx = (this.thispage - 1) * this.pageSize
item.index = startIdx + (index + 1)
let _id = index
inhtml += `