轉自: 牛老師~https://blog.csdn.net/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裡顯示的。
請各路大神批評指正~~~