ArcGIS API For JavaScript(六)InfoWindow

ArcGIS API For JavaScript(InfoWindow

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">

<title>InfoWindow简单示例</title>

<link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/tundra/tundra.css" />

<link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css" />

 

<style>

html, body, #mapDiv {

padding:0;

margin:0;

height:100%;

}

#mapDiv {

position: relative;

}

#info {

background: #fff;

box-shadow: 0 0 5px #888;

left: 1em;

padding: 0.5em;

position: absolute;

top: 1em;

z-index: 40;

}

</style>

 

<script src="http://js.arcgis.com/3.9/"></script>

<script>

var map, dialog;

require([

"esri/map", "esri/layers/FeatureLayer",

"esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol",

"esri/renderers/SimpleRenderer", "esri/graphic", "esri/lang",

"esri/Color", "dojo/number", "dojo/dom-style",

"dijit/TooltipDialog", "dijit/popup", "dojo/domReady!"

], function(

Map, FeatureLayer,

SimpleFillSymbol, SimpleLineSymbol,

SimpleRenderer, Graphic, esriLang,

Color, number, domStyle,

TooltipDialog, dijitPopup

) {

map = new Map("mapDiv", { //设置底图

basemap: "osm",

center: [-80.94, 33.646],

zoom: 8,

slider: false //不设置滑动条

});

 

var southCarolinaCounties = new FeatureLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3",

{

mode: FeatureLayer.MODE_SNAPSHOT,

outFields: ["NAME", "POP2000", "POP2007", "POP00_SQMI", "POP07_SQMI"]

});

//定义要素图层,设置模式和字段

southCarolinaCounties.setDefinitionExpression("STATE_NAME = 'South Carolina'"); //设置过滤条件

 

var symbol = new SimpleFillSymbol(

SimpleFillSymbol.STYLE_SOLID,

new SimpleLineSymbol(

SimpleLineSymbol.STYLE_SOLID,

new Color([255,255,255,0.35]),

1

),

new Color([50,125,125,0.35])

);

southCarolinaCounties.setRenderer(new SimpleRenderer(symbol)); //要素图层渲染

map.addLayer(southCarolinaCounties); //添加图层

map.infoWindow.resize(245,125); //设置地图信息窗的大小

dialog = new TooltipDialog({ //工具提示对话框,设置样式

id: "tooltipDialog",

style: "position: absolute; width: 250px; font: normal normal normal 10pt Helvetica;z-index:100"

});

dialog.startup(); //启用对话框

var highlightSymbol = new SimpleFillSymbol(

SimpleFillSymbol.STYLE_SOLID,

new SimpleLineSymbol(

SimpleLineSymbol.STYLE_SOLID,

new Color([255,0,0]), 3

),

new Color([125,125,125,0.35])

);

//close the dialog when the mouse leaves the highlight graphic

map.on("load", function(){ //地图加载完成后启用图形鼠标事件,当鼠标移出时关闭对话框

map.graphics.enableMouseEvents(); //Provides access to the Map's GraphicsLayer. The graphics object is available to use after the Map.onLoad event.

map.graphics.on("mouse-out", closeDialog);

});

//listen for when the onMouseOver event fires on the countiesGraphicsLayer

//when fired, create a new graphic with the geometry from the event.graphic and add it to the maps graphics layer

southCarolinaCounties.on("mouse-over", function(evt){ //为要素图层添加鼠标over事件

var t = "<b>${NAME}</b><hr>" //在这里t是模板的缩略,这个模板使用了通配符来过滤要显示的字段和属性

+ "<b>2000 Population: </b>${POP2000:NumberFormat}<br>" 

+ "<b>2000 Population per Sq. Mi.: </b>${POP00_SQMI:NumberFormat}<br>"

+ "<b>2007 Population: </b>${POP2007:NumberFormat}<br>"

+ "<b>2007 Population per Sq. Mi.: </b>${POP07_SQMI:NumberFormat}";

var content = esriLang.substitute(evt.graphic.attributes,t); //将上一步定义的模板替换到目标数据中,目标数据是鼠标所在图形

var highlightGraphic = new Graphic(evt.graphic.geometry,highlightSymbol);//将鼠标所在的图形用高亮显示符号渲染

map.graphics.add(highlightGraphic); //添加图形

dialog.setContent(content); //设置对话框内容

domStyle.set(dialog.domNode, "opacity", 0.85); //设置对话框透明度0.85

dijitPopup.open({ // dijit.popup.open({ popup: tooltipDialog, around: node }); 打开提示框

popup: dialog,

x: evt.pageX,

y: evt.pageY

});

});

function closeDialog() {

map.graphics.clear(); //清除图形

dijitPopup.close(dialog); //关闭对话框

}

});

</script>

</head>

<body class="tundra">

<div id="mapDiv">

<div id="info">

Hover over a county in South Carolina to get more information.

</div>

</div>

</body>

</html>


地理信息科学

Writed By NX

QQ:1051926720