转自: 牛老师~https://blog.youkuaiyun.com/gisshixisheng/article/details/27328731?utm_source=tuicool
一、前言
地图开发中弹出气泡框,使用esri原生的简直是┭┮﹏┭┮,所以还是自已写一个吧。
主要是继承InfoWindowBase模块。现定义一个模块InfoWindow(抄袭人家的名字,哈哈~~)
二、整个文件 InfoWindow.js如下:
define([
"dojo/Evented",
"dojo/parser",
"dojo/on",
"dojo/_base/declare",
"dojo/dom-construct",
"dojo/_base/array",
"dojo/dom-style",
"dojo/_base/lang",
"dojo/dom-class",
"dojo/fx",
"dojo/Deferred",
"esri/domUtils",
"esri/InfoWindowBase"
],
function(
Evented,
parser,
on,
declare,
domConstruct,
array,
domStyle,
lang,
domClass,
coreFx,
Deferred,
domUtils,
InfoWindowBase
)
{
var infoWidth =300,infoHeight =200;
var initMapCenter,initScreenCenter;
var showMapPoint =null,showScreenPoint=null;
return declare([InfoWindowBase, Evented],
{
constructor: function(parameters)
{
lang.mixin(this, parameters);
domClass.add(this.domNode, "myInfoWindow");
this._closeButton = domConstruct.create("div",{"class": "info-close", "title": "关闭"}, this.domNode);
this._title = domConstruct.create("div",{"class": "info-title"}, this.domNode);
this._content = domConstruct.create("div",{"class": "info-content"}, this.domNode);
this._arrow = domConstruct.create("div",{"class": "info-arrow"}, this.domNode);
on(this._closeButton, "click", lang.hitch(this, function(){
this.hide();
}));
domUtils.hide(this.domNode);
this.isShowing = false;
},
setMap: function(map)
{
this.inherited(arguments);
map.on("pan", lang.hitch(this, function(pan){
var movePoint=pan.delta;
if(this.isShowing)
{
if(showScreenPoint!=null)
{
this._showInfoWindow(showScreenPoint.x+movePoint.x,showScreenPoint.y+movePoint.y);
}
}
}));
map.on("pan-end", lang.hitch(this, function(panend){
var movedelta=panend.delta;
if(this.isShowing){
showScreenPoint.x=showScreenPoint.x+movedelta.x;
showScreenPoint.y=showScreenPoint.y+movedelta.y;
}
}));
map.on("zoom-start", lang.hitch(this, function(){
domUtils.hide(this.domNode);
this.onHide();
}));
map.on("zoom-end", lang.hitch(this, function(){
if(this.isShowing){
showScreenPoint=this.map.toScreen(showMapPoint);
this._showInfoWindow(showScreenPoint.x,showScreenPoint.y);
}
}));
},
setTitle: function(title){
this.place(title, this._title);
},
setContent: function(content){
this.place(content, this._content);
},
_showInfoWindow:function(x,y)
{
domStyle.set(this.domNode,{
"left": x - infoWidth/2 +this.offset.x + "px",
"top": y - infoHeight-20 -this.offset.y + "px"
});
domUtils.show(this.domNode);
},
show: function(location,offset){
this.offset = dojo.mixin({x:0,y:0},offset);
showMapPoint=location;
initMapCenter=this.map.extent.getCenter();
initScreenCenter=this.map.toScreen(initMapCenter);
infoHeight= $(".myInfoWindow").height();
infoWidth= $(".myInfoWindow").width();
if(infoWidth<20||infoHeight<20){
this.resize(300,200);
}
if(location.spatialReference){
location = this.map.toScreen(location);
}
var left=location.x-infoWidth/2;
var top=location.y-infoHeight-75;
showScreenPoint=location;
if(top<5){
initScreenCenter.y=initScreenCenter.y+top-5;
}
if(left<5){
initScreenCenter.x=initScreenCenter.x+left-5;
}
this._showInfoWindow(showScreenPoint.x,showScreenPoint.y);
/*initMapCenter=this.map.toMap(initScreenCenter);
this.map.centerAt(initMapCenter);*/
this.isShowing = true;
this.onShow();
},
hide: function(){
domUtils.hide(this.domNode);
this.isShowing = false;
this.onHide();
},
resize: function(width, height){
domStyle.set(this._content,{
"width": width + "px",
"height": (height-60) + "px"
});
domStyle.set(this._title,{
"width": width + "px"
});
},
destroy: function(){
domConstruct.destroy(this.domNode);
this._closeButton = this._title = this._content = null;
}
});
return InfoWindow;
});
说人家的东西丑,你拿个好看的来啊(勉强看一下,请大神们轻喷~),infoWindow.css掏出来:
.myInfoWindow {
position: absolute;
z-index: 100;
font-family: sans-serif;
font-size: 12px;
background-color:#ffffff;
border-radius:10px;
}
.info-content {
border-radius:10px;
position: relative;
background-color:#ffffff;
color:#353535;
overflow: auto;
}
.info-close {
position: absolute; top: 5px; right: 5px;
cursor: pointer;
background: url(../images/infowindow_close.png) no-repeat scroll 0 0 transparent;
width: 12px;
height: 12px;
}
.info-close:hover {
background-color: red;
}
.info-title {
border-radius:10px;
font-weight: bold;
background-color:#4774d9;
color:#ffffff;
height:20px;
}
.info-arrow {
width: 0;
height: 0;
position: absolute;
bottom: -15px;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid #ffffff;
margin-left: calc(50% - 15px);
}
调用示意:
用它就把它引入到你要调用的模块中,
然后创建一个对象
var infoWindow = new InfoWindow({domNode: domConstruct.create("div", null, dom.byId("mapdiv"))});
map = new Map("mapdiv",{
logo:false,
sliderPosition:"top-right",
infoWindow : infoWindow
});
这里简单说明一下(私人理解),infoWindow和infoTemplate,
infoWindow:顾名思义它是个窗口,是气泡框的父体,你可以setTitle(),setContent();但是里面的内容需要完全拼接;
infoTemplate: 故啥思啥它是个模板,通过 '$' 符可以属性值,前提是你在创建graphic的时候赋值属性了,最终展示是在 infoWindow里显示的。
请各路大神批评指正~~~