lzugis——Arcgis Server for JavaScript API之自定义InfoWindow

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

在前面的两篇相关的文章里面,实现InfoWindow是通过div的东西实现的,本文要讲的是通过集成InfoWindowBase实现infowindow的。实现后InfoWindow主要修改了arcgis原来的样式,并加入了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,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;
});

InfoWindow.js

.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;
	width: 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;
  width: 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%; width: 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>

下载源代码

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏JavaEdge

CSS 全解析实战(八)-Bootstrap1 Bootstrap介绍2 Bootstrap基本用法3 Bootstrap JS组件

12320
来自专栏运维前线

Dell服务器常用管理命令总结

Dell服务器常用管理命令总结 准备新版本的 MegaCli-8.07.14-1.noarch.rpm 会把程序安装在/opt下,可以自定义安装目录,例如: ...

27370
来自专栏转载gongluck的CSDN博客

第17章 ioctl操作

#include <sys/ioctl.h> int ioctl(int fd, unsigned long request, ...); /usr/src/...

39090
来自专栏闵开慧

NameError: name 'admin' is not defined(彻底解决方案)

1 查看model.py文件,具体操作如: root@ubuntu118:/home/python/work/mysite/mysite/blo...

45170
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

27490
来自专栏有困难要上,没有困难创造困难也要上!

Linux查看进程栈信息

43260
来自专栏机器人网

机器人变位机减速器选型

1、 实心型RV减速器型号:日本帝人RV减速器第一种:实心型RV-40E-105-B-B 伺服电机与减速器连接轴连接采用右下图所示: ? 2、 空心型RV减速器...

30250
来自专栏DeveWork

几款更换WordPress 后台UI 的插件推荐

WordPress 3.8出来了,最大的改变就是全新扁平化后台UI。如果你对这个后台UI 不怎么感冒甚至有些反感(尼玛怎么那么丑啊?!)不妨用下面的插件来尽可能...

39970
来自专栏IMWeb前端团队

Zone.js 简介 & 抛砖引玉

本文作者:IMWeb 杨文坚 原文出处:IMWeb社区 未经同意,禁止转载 Zone.js是angular团队参照NodeJS的Domain,Dart...

24660
来自专栏鸿的学习笔记

python源码阅读笔记之线程机制

15230

扫码关注云+社区

领取腾讯云代金券