Arcgis Server for JavaScript API之自定义InfoWindow

摘要:
当你看到这个标题时不要感到不安,因为我最近一直在研究相关问题,所以相关文章只能是这些。同时,我希望看过我文章的朋友能帮助你。在前两篇相关文章中,InfoWindow是通过div实现的。本文将讨论如何通过集成InfoWindowBase来实现InfoWindow。实现后,InfoWindow主要修改了arcgis的原始样式,并添加了InfoWindow越界处理。

各位看到这个标题不要嫌烦,因为本人最近一直在研究相关的问题,所以相关文章也只能是这些,同时希望看过我的文章的朋友,我的文章能够给你帮助。

在前面的两篇相关的文章里面,实现InfoWindow是通过div的东西实现的,本文要讲的是通过集成InfoWindowBase实现infowindow的。实现后InfoWindow主要修改了arcgis原来的样式,并加入了InfoWindow出界的处理。

源代码奉上:

myInfoWindow/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,infoHeight;
  var initMapCenter,initScreenCenter;
  var showMapPoint,showScreenPoint=null;
  
  return declare([InfoWindowBase, Evented], 
  {
  constructor: function(parameters) 
    {
      lang.mixin(this, parameters);
      domClass.add(this.domNode, "myInfoWindow");
      this._closeButton = domConstruct.create("div",{"class": "close", "title": "关闭"}, this.domNode);
      this._title = domConstruct.create("div",{"class": "title"}, this.domNode);
      this._content = domConstruct.create("div",{"class": "content"}, this.domNode);
      this._arrow = domConstruct.create("div",{"class": "arrow"}, this.domNode);
      on(this._closeButton, "click", lang.hitch(this, function(){
        //hide the content when the info window is toggled close.
        this.hide(); 
      }));
      //hide initial display 
      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)
    {
      //Position 10x10 pixels away from the specified location
      domStyle.set(this.domNode,{
        "left": x - infoWidth/2 + 15 + "px",
        "top": y - infoHeight-75 + "px"
      });
      //display the info window
      domUtils.show(this.domNode); 
    },
  show: function(location)
    {
      showMapPoint=location;
      
      initMapCenter=this.map.extent.getCenter();
      initScreenCenter=this.map.toScreen(initMapCenter);
      
      infoHeight= $(".myInfoWindow").height();
      infoWidth= $(".myInfoWindow").width();
      
      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;
});

myInfoWindow/InfoWindow.css

.myInfoWindow {
  position: absolute;
  z-index: 100;
  font-family: sans-serif;
  font-size: 12px;
}

.dj_ie .myInfoWindow {
  border: 1px solid black;
}

.myInfoWindow .content {
  position: relative;
  background-color:#EFECCA;
  color:#002F2F;
  overflow: auto;
  padding-top:5px;
  padding-bottom:5px;
  padding-left:5px;
}

.myInfoWindow .arrow {
  position: absolute;
   0px;
  height: 0px;
  line-height: 0px;/*为了防止ie下出现题型*/
  border-top: 60px solid #EFECCA;
  border-left: 5px solid transparent;
  border-right: 20px solid transparent;
  left: 45%;
  bottom: -60px;
}

.myInfoWindow .close {
  position: absolute; top: 7px; right: 5px;
  cursor: pointer;
  background: url(http://serverapi.arcgisonline.com/jsapi/arcgis/2.6/js/dojo/dijit/themes/claro/layout/images/tabClose.png) no-repeat scroll 0 0 transparent;
   12px; height: 12px;
}

.myInfoWindow .close:hover  {
  background-color: #F7FCFF;
}

.myInfoWindow .title {
  font-weight: bold;
  background-color:#046380;
  color:#E6E2AF;
  padding-top:5px;
  padding-bottom:5px;
  padding-left:5px;
}

test.html

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!--The viewport meta tag is used to improve the presentation and behavior
    of the samples on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Custom Info Window</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
    <link rel="stylesheet" href="myModules/InfoWindow.css">
    <style>
      html, body, #mapDiv { height: 100%;  100%; margin: 0; padding: 0; } 
  
    </style>

    <script>
  var dojoConfig = {
        parseOnLoad:true,
        packages: [{
          "name": "myModules",
          "location": location.pathname.replace(//[^/]+$/, "") + "/myModules"
        }]
      };
    </script>
    <script src="http://localhost/arcgis_js_api/library/3.9/3.9/init.js"></script>
    <script src="jquery.min.js"></script>
    <script>

    require([
      "dojo/dom",
      "dojo/dom-construct",
      "esri/map", 
      "myModules/InfoWindow",
      "esri/layers/ArcGISTiledMapServiceLayer",
      "esri/symbols/PictureMarkerSymbol",//图片点符号
      "esri/renderers/SimpleRenderer", //简单渲染
      "esri/layers/FeatureLayer",
      "esri/InfoTemplate",
      "dojo/string",
      "dojo/domReady!"
    ], function(
       dom,
       domConstruct,
       Map,
       InfoWindow,
       ArcGISTiledMapServiceLayer,
       PictureMarkerSymbol,
       SimpleRenderer,
       FeatureLayer,
       InfoTemplate,
       string
    ) {
      //create the custom info window specifying any input options
       var infoWindow = new  InfoWindow({
          domNode: domConstruct.create("div", null, dom.byId("mapDiv"))
       });

      var map = new Map("mapDiv", {
          logo:false,
          basemap: "gray",
          center: [-98.57, 39.82],
          zoom: 4,
          zoom: 4,
          slider: true,
      	  infoWindow: infoWindow
      });			

      //define the info template that is used to display the popup content. 
      var template = new InfoTemplate();
      template.setTitle("<b>${name}</b>");
      template.setContent("hello");
      template.setContent("<h1>我是中国人民的儿子</h1><br>你妹啊!!!"); 

    var featurelayercity = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Boston_Marathon/FeatureServer/0", {
          mode: FeatureLayer.MODE_SNAPSHOT,
          infoTemplate: template,
      outFields: ["*"]
      });
    var pmsRed = new PictureMarkerSymbol('../images/location_icon_blue.png', 20, 20).setOffset(0, 15);
    //简单渲染
    var sr=new SimpleRenderer(pmsRed);
    featurelayercity.setRenderer(sr);		
      map.addLayer(featurelayercity);	  

      //resize the info window 
      map.infoWindow.resize(400, 200);

    });
    </script>
  </head>
  <body>
    <div id="mapDiv"></div>
  </body>
</html>

免责声明:文章转载自《Arcgis Server for JavaScript API之自定义InfoWindow》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android中SD卡内容读取和简易FTP文件上传(番外)Latex多行公式的处理[转]下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ArcGIS AddIn开发笔记(一)

学习AddIn开发,遇到了些稀奇古怪的问题,网上的资料少之又少。 (1)AddIn开发,主要是通过ArcMap静态变量,与主程序中的数据等进行交互 (2)failed to register Add In .esriAddIn异常,此类异常为Visual Studio中项目名称或者类名称中出现了中文名 (3)AddIn个人觉得很不稳定,第一次写完一个Too...

ArcGIS地图文档(mxd)过大的问题

  今天在作图的时候无意发现了一个问题,mxd文档居然有6MB多,原先以为是符号库引起的,就移除了加载的所有符号库,可文件体积并没有发生大的变化。上网搜了一下esri的论坛,才发现这个问题原来是geoprocessing引起的,只要在arcmap中执行过工具,就会把工具执行的结果保存在mxd里,因此才造成了文件大的离谱。      解决方法:点击geopr...

gp 服务的发布与javascript调用

最近在学习ArcGIS 10.1 for Server 相关的知识,其中的一个必然掌握的知识点,就是服务的发布与使用。在ArcGIS Server 10.1 支持多种服务,包括:地图服务,影像服务等,而gp服务是日常使用最为广泛的服务。 扩展阅读:Esri的server的官方帮助中的服务类型的内容 http://resources.arcgis.com/z...

(四)WebGIS中通过行列号来换算出多种瓦片的URL 之离线地图

文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.前言 在前面我花了两个篇幅来讲解行列号的获取,也解释了为什么要获取行列号。在这一章,我将把常见的几种请求瓦片时的URL样式罗列出来,并且给出大致的解释。 我在这里将地图分为离线地图和在线地图。所谓离线地图,即保存在本地而没有发...

arcengine动态显示所需字段值

需求:实现和GIS桌面端中Identify的类似功能,鼠标滑动的时候可以显示鼠标所在位置的要素的指定字段的值.。 主要操作流程: ①先打开一个对话框,用于选择需要显示的图层和字段名 ②点击确定之后,在mapControl上鼠标滑动的时候利用axMapControl的showTips功能实现实时显示,相对于Identify,取消的点击查看属性,和只能查看所有...

ArcGIS地图打印设置

1.需求:客户自己开发的Engine程序,调用的是LayoutControl,需要连接大型绘图仪进行出图。     业务流程是先框选要打印的地图范围,该范围是自定义大小,框选完成之后进行预览,然后选择打印输出,在打印输出界面进行设置后,由惠普绘图仪进行出图。 2.出现的问题:惠普打印机里用的是卷筒纸,宽为定值,长度可以无限延长,客户说纸张大小是A1的大小。...