1.引言

  由于Arcgis for javascript API不可以绘制Geojson,并且提供的Popup一般只可以弹出一个,在很多专题图制作中,会遇到不少的麻烦。因此本文结合了两个现有的Arcgis for javascript API扩充库,对其进行改造达到绘制Geojson并同时弹出多个Popup的目的。

目前已有的两个扩充库github地址(可供单独使用):

1.绘制Geojson的扩充库:https://github.com/Esri/geojson-layer-js

2.多个Popup显示的扩充库:https://github.com/nickcam/PopupExtended

  本文实现的效果图:

图1 上海5个地点的部分预报属性                                 图2 上海某三条航线的部分预报属性

2. 各类依赖库引入及前端HTML

  首先需要先载入需要用的常用js、Arcgis及两个扩充库的js及部分css(下载地址见其github):

<!DOCTYPE html>
<html>
<head><title>Add GeoJSON and Display Multiple Popup</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=7,IE=9"><meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"><!-- ArcGIS API for JavaScript CSS--><link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css"><link rel="stylesheet" href="http://js.arcgis.com/3.13/dijit/themes/claro/claro.css"><!-- Web Framework CSS - Bootstrap (getbootstrap.com) and Bootstrap-map-js (github.com/esri/bootstrap-map-js) --><link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"><link rel="stylesheet" href="./css/bootstrap.min.css"><!-- PopExtendCss --><link href="./vendor/ncam/PopupExtended.css" rel="stylesheet" /><!-- PopupExtended references --><script>var dojoConfig = {parseOnLoad: false,async: true,tlmSiblingOfDojo: false,packages: [{name: "ncam",location: location.pathname.replace(/\/[^/]+$/, '') + "ncam"
            }]};</script><!-- ArcGIS API for JavaScript library references --><script src="//js.arcgis.com/3.10"></script><!-- Terraformer reference --><script src="./vendor/terraformer/terraformer.min.js"></script>    <script src="./vendor/terraformer-arcgis-parser/terraformer-arcgis-parser.min.js"></script><!-- other reference --><script src="./vendor/jquery.js"></script>
</head>
<body></body>
</html>

  加入底图所需要的div与图层切换的Button:

<body><div id="mapDiv"></div><button type="line" id="shanghaiPoint" class="btn btn-default buttonRight" style="top:20px;right:20px">上海区各点</button><button type="point" id="threeLine" class="btn btn-default buttonRight" style="top:70px;right:20px">三条航线</button>
</body>

3.置入Popupextended并扩充geojsonlayer.js  

  然后从geojsonlayer.js源码入手,开始将PopupExtended扩展其中,让我们新构建的geojsonlayer直接可拥有多个Popup。在geojsonlayer.js的constructor中很容易可以找出infotemplate的set方法:

            // Default popupif (options.infoTemplate !== false) {this.setInfoTemplate(options.infoTemplate || new InfoTemplate("GeoJSON Data", "${*}"));}

  很明显,geojsonlayer初始化时通过options传入参数进行判断并构造,所以实现本文目的的大致思路是将这里的setInfoTemplate替换成可以扩展的PopupExtended:

if (options.infoTemplate !== false) {//① create a PopupTemplatevar template = new PopupTemplate({title: "{name}",fieldInfos: [{ fieldName: "Id", label: "Id", visible: true },{ fieldName: "publishdate", label: "观测日期", visible: true },{ fieldName: "waveheight", label: "浪高", visible: true },{ fieldName: "wavedirection", label: "浪向", visible: true },{ fieldName: "windspeed", label: "风速", visible: true },{ fieldName: "winddirection", label: "风向", visible: true },{ fieldName: "comfort", label: "等级", visible: true }],extended: { actions: [{ text: " IconText", className: "iconText", title: "Custom action with an icon and Text", click: function (feature) { alert("Icon Text clicked on " + "id: " + feature.attributes.id + " " + feature.attributes.name); } },{ text: "", className: "iconOnly", title: "Custom action only using an icon", click: function (feature) { alert("Icon action clicked on " + "id: " + feature.attributes.id + " " + feature.attributes.name); } }                          ],//uses a pretty bad custom theme defined in PopupExtended.css.scaleSelected: 1.6}});//② create a extend for basemapvar extendedPopup = new PopupExtended({extended: {themeClass: "light",draggable: true,defaultWidth: 250,actions: [{text: "其他", className: "defaultAction", title: "Default action added in extendedPopup properties.",click: function (feature) { alert("clicked feature - " + feature.attributes); }}],hideOnOffClick: false,multiple: true,},highlight: false,//titleInBody: false,}, dojo.create("div"));//③set the map to use the exteneded popup
                extendedPopup.setMap(options.baseMap);options.baseMap.infoWindow = extendedPopup;this.setInfoTemplate(options.infoTemplate || template);}

  由上段代码可见,引入Popup给GeoJSON共分为三步:①实例化一个你需要的PopupTemplate(这里起名为template),可指定你需要展示的主题、数据项及扩展的一些交互action;②实例化一个PopupExtended并设置一些默认的Popup属性;③将实例化的PopupExtended——extendedPopup的Map设置为baseMap,并将baseMap的infowindow设置为extendedPopup,最后将geojsonlayer的infoTemplate设置为新构建的template。这样便可以实现对置入底图的geojsonlayer进行多个infoTemplate展示需求了。源代码见github:展示效果如图3:

图3 对geojsonlayer扩充Popupextended后的显示效果

  如若只需增加多个Popup至geojsonlayer的话,以上部分足以实现了。

4.增加新的Attributes及调整Popup的样式  

  由于设计上的需求,笔者需要从其他地址获取观测点的部分观测值,并且笔者的老师觉得应该加一些icon给属性,美化展示效果,所以需要重新构造两部分:①获取并为graphics增加新的attributes;②重构geojsonlayer的infoTemplate的content。

4.1 获取并为graphics增加新的attributes:

  通过在button上利用fetch及Promise.all来同时获取6个点或3条航线的数据,并传入至初始化geojsonlayer的函数内;

$("#shanghaiPoint").click(function(){// if first init geojsonlayerif(firstPointInit){var requestBZ = 'http://wx.dhybzx.org:18080/forecast/shanghai_sea_env/80';var requestWGQ = 'http://wx.dhybzx.org:18080/forecast/shanghai_sea_env/81';var requestHS = 'http://wx.dhybzx.org:18080/forecast/shanghai_sea_env/82';var requestLCG = 'http://wx.dhybzx.org:18080/forecast/shanghai_sea_env/83';var requestJHG = 'http://wx.dhybzx.org:18080/forecast/shanghai_sea_env/84';var requestJSW = 'http://wx.dhybzx.org:18080/forecast/shanghai_sea_env/85';var urls = [requestBZ, requestWGQ,requestHS,requestLCG,requestJHG,requestJSW]Promise.all(urls.map(url =>fetch(url).then(resp => resp.json()))).then(results => {var tempJson = {"堡镇":results[0][0],"外高桥":results[1][0],"横沙":results[2][0],"芦潮港":results[3][0],"金汇港":results[4][0],"金山卫":results[5][0]}addGeoJsonToMap("./data/six_point.json",tempJson)});}else{//geojsonlayer has been initialaddGeoJsonToMap("./data/six_point.json")}})

  这里的Promise.all采用了ES2015的箭头函数,兼容性问题需要自己考虑,也可以手动改成ES5支持的。将额外的attributes组装成tempJson后传入至初始化方法addGeoJsonToMap内。

4.2 重构geojsonlayer的infoTemplate的content:

  在geojsonlayer.js内继续做一部分修改,注释掉实例化template中的fieldInfos属性及值,并且为geojsonlayer的infoTemplate设置新的content,代码如下:

               var template = new PopupTemplate({title: "{name}",// fieldInfos: [//     { fieldName: "Id", label: "Id", visible: true },//     { fieldName: "publishdate", label: "观测日期", visible: true },//     { fieldName: "waveheight", label: "浪高", visible: true },//     { fieldName: "wavedirection", label: "浪向", visible: true },//     { fieldName: "windspeed", label: "风速", visible: true },//     { fieldName: "winddirection", label: "风向", visible: true },//     { fieldName: "comfort", label: "等级", visible: true }// ],
                        extended: { actions: [{ text: " IconText", className: "iconText", title: "Custom action with an icon and Text", click: function (feature) { alert("Icon Text clicked on " + "id: " + feature.attributes.id + " " + feature.attributes.name); } },{ text: "", className: "iconOnly", title: "Custom action only using an icon", click: function (feature) { alert("Icon action clicked on " + "id: " + feature.attributes.id + " " + feature.attributes.name); } }                          ],//uses a pretty bad custom theme defined in PopupExtended.css.scaleSelected: 1.6}});//create a extend for basemapvar extendedPopup = new PopupExtended({extended: {themeClass: "light",draggable: true,defaultWidth: 250,actions: [{text: "其他", className: "defaultAction", title: "Default action added in extendedPopup properties.",click: function (feature) { alert("clicked feature - " + feature.attributes); }}],hideOnOffClick: false,multiple: true,},highlight: false,//titleInBody: false,}, dojo.create("div"));//set the map to use the exteneded popup
                extendedPopup.setMap(options.baseMap);options.baseMap.infoWindow = extendedPopup;this.setInfoTemplate(options.infoTemplate || template);this.infoTemplate.setContent("<b class='popupTitle'>${name}</b>" +"<div class='hzLine'></div>"+"<div class='popupContent'>"+"<i class='glyphicon glyphicon-calendar'></i><b>日期: </b> ${publishdate}<br/>"+"<i class='glyphicon glyphicon-resize-vertical'></i><b>浪高: </b> ${waveheight}<br/>" +"<i class='glyphicon glyphicon-random'></i><b>浪向: </b> ${wavedirection}<br/>"+"<i class='glyphicon glyphicon-share'></i><b>风速: </b> ${windspeed}<br/>" +"<i class='glyphicon glyphicon-transfer'></i><b>风向: </b> ${winddirection}<br/>"+"<i class='glyphicon glyphicon-export'></i><b>等级: </b> ${comfort}<br/>"+"</div>");

  额外的属性和新的infoTemplate样式构造完成,但存在一个问题,即额外的attributes必须要在geojsonlayer绘制好后再进行设置并展示,arcgis提供了layer的layer-add及layer-add-result事件,但是无法监控到graphics是否已经增入至geojsonlayer内,所以必须要再做一些改进,使额外的属性能够在graphics绘制完毕后再添加进去。具体方法分为两步:1)初始化geojsonlayer时,将showAllPopup方法传入其构造函数内;2)在grahics添加至layer后,调用showAllPopup方法,显示所有的Popup。前端代码如下:

//add GeoJSON to baseMap , constuct show&hide popup method and add other attribute to graphicsfunction addGeoJsonToMap(url,otherJson){require(["esri/map","./src/geojsonlayer.js","esri/geometry/Point", "esri/SpatialReference","dojo/on","dojo/dom","dojo/domReady!"],function (Map, GeoJsonLayer, Point, SpatialReference,on, dom) {var hasThisLayer=false;otherJson=otherJson?otherJson:"";hideAllPopup()//judge layer has been initmap.getLayersVisibleAtScale().forEach(function(item){if(item._url==url&&item.dataType=="geojson"){console.log(item)item.show();console.log("dd")showAllPopup(item);hasThisLayer=true;// map.setExtent(item.extent)}else if(item._url!=url&&item.dataType=="geojson"){item.hide();}})if(!hasThisLayer){addGeoJsonLayer(url);                                  }//show all popupsfunction showAllPopup(layer){......}//hide all popupsfunction hideAllPopup(){.......}//add other attribute to grpahics for popupfunction addAttrToGrpahics(item,type){.......}// Add the layerfunction addGeoJsonLayer(url) {// Create the layervar geoJsonLayer = new GeoJsonLayer({baseMap:map,url: url,onLayerLoaded:function(layer){showAllPopup(layer);}              });// Add to mapgeoJsonLayer.dataType="geojson"; map.addLayer(geoJsonLayer);}});}

并且在geojsonlayer.js的constructor内加入:

this._onLayerLoaded = options.onLayerLoaded;

在最后的_addGraphics方法中onLoad方法后,加入:

if (this._onLayerLoaded) this._onLayerLoaded(this);

利用show/hide方法,控制popup显示及隐藏。

//open all popup
layer.graphics.forEach(function(item){if(firstPointInit&&otherJson[item.attributes.name]){addAttrToGrpahics(item,layer.graphics[0].geometry.type)}var loc = map.toScreen(item.geometry);map.infoWindow.setFeatures([item]);map.infoWindow.show(loc);
})
//hide all popup
var tempLength = map.infoWindow.openPopups.length;
for(var i=0;i<tempLength;i++){map.infoWindow.openPopups[0].hide()
}

5. 结论  

  至此,本文已经完成了在Arcgis for javascript API中实现Geojson的绘制,并同时展示其多个Popup的需求。最终的展示效果如图1、2。源代码见笔者的github:https://github.com/EasonXu818/Add-GeoJSON-Multiple-Popups。

转载于:https://www.cnblogs.com/easonxu/p/6588529.html

利用Arcgis for javascript API绘制GeoJSON并同时弹出多个Popup相关推荐

  1. Java经纬度画轨迹图_利用百度地图JavaScript API绘制运动轨迹

    写在前面 欢迎访问我的博客 如果需要使用百度地图javasript api,需先进行开发者认证,balalalala写上一堆理由之后认证就行,然后创建浏览器端的应用,得到可用的Ak,详情见百度地图开放 ...

  2. ArcGIS For JavaScript API 默认参数

    "esri.config"的是在1.3版中的的"esriConfig"的替代品.如果您使用的是1.2或更低的版本,您应该参阅默认API v1.2和更低的配置.对 ...

  3. Arcgis server Javascript API 官网相当于HelloWorld的完整版

    虽然放暑假了,不过最近依然很忙,一边忙着准备电子设计大赛,一边跟随研究生师兄们要做Arcgis相关内容,看了一本ArcGIS Server JavaScript API的书,结果发现是针对Arcgis ...

  4. javascript入门系列演示·三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  5. Vue项目流程7,交易页面,提交订单,支付页面,利用element UI 以及 QRCode 完成微信支付,弹出框按钮的相关工作,个人中心以及子路由我的订单

    目录 交易页面 提交订单 支付页面 微信支付 个人中心 我的订单 交易页面 1.静态组件及路由跳转 2.获取交易页数据 (1)接口 //获取用户地址信息 地址:/api/user/userAddres ...

  6. ArcGis For JavaScript API Drawing Tool (绘图工具又称框选地图)

    描述: 您可以使用绘图工具栏绘制在地图上的多种几何形状.. 在线演示:http://help.arcgis.com/en/webapi/javascript/arcgis/samples/toolba ...

  7. 【天地图】使用天地图api绘制GeoJson数据

    天地图没有直接提供加载GeoJson数据或者文件的api,但是我们可以借助绘制多边形的方式实现 1 实现效果 绘制如下的矢量多边形效果: 2 实现技术 (1) 首先用ajax请求geojson文件,从 ...

  8. ArcGis For JavaScript API (测量工具)

    测量组件提供工具,用于计算当前位置(获取位置),测量距离测量距离:面积 var  measurement  = new  esri .迪吉特.测量({     地图: 地图}, 道场.byId ('m ...

  9. 百度地图API禁用点击景点弹出详细信息的方法

    一般默认创建地图的方式使用: var map = new BMap.Map("allmap"); 这种创建方式默认是开启了点击景点的事件, 如需要禁用点击景点弹出详细信息窗口则使用 ...

最新文章

  1. aspose.words 表格内容水平居中_CSS十五种方法教你如何居中一个元素
  2. conj--复数的共轭值
  3. vue tabs 动态组件
  4. Groovy里读写本地文件的几种方式
  5. Java Enum学习总结
  6. python如何关闭multiprocess_python 开启进程两种方法 multiprocessing模块 介绍
  7. 分割、检测与定位,高分辨率网络显神威!这会是席卷深度学习的通用结构吗?...
  8. NOIP201501金币
  9. lesson6 复数及复指数
  10. Hibernate 与 Mybatis 如何共存?打破你的认知!
  11. 松翰单片机之汇编编程
  12. 单机版传奇传奇服务端架设传奇服务器架设
  13. 清华大学计算机系前景好么,华为正式宣布选择西工大,而不是清华计算机系,网友:为什么?...
  14. Safari 手机浏览器无法下载的问题
  15. XDOJ 1202: The Offer - Lunatic
  16. 使用ANSI控制字体颜色
  17. 2021-2025年中国自攻螺丝行业市场供需与战略研究报告
  18. R语言中 layout函数
  19. vue和SpringBoot搭建项目(原创)
  20. 基于java二手商品交易平台设计与实现

热门文章

  1. 正方体最快最简单画_素描新手入门第一幅画可不只是“正方体”
  2. 3dobject用什么打开_第一次用开塞露是什么感觉?网友:像打开了新世界的大门
  3. pccad自定义图框_(PCCAD自定义标题栏详细方法.doc
  4. linux静默删除文件夹,Linux常用命令10 - unzip
  5. mybatis oracle trim,Mybatis trim标签
  6. webase crud查看所有表_Laravel-Gii 可视化代码生成工具 CRUD +GUI
  7. 梯度下降法预测波士顿房价以及简单的模型评估
  8. 一个很好的自学网站~推荐一下
  9. 分析FLV文件分析和解析器的开源代码
  10. 汇编语言-007(ADD_SUB_NEG 、 PUSH和POP指令应用 、 AND,OR,XOR使用 、 条件跳转应用)