Acrgis jsapi 地图基本属性 事件 工具 测距等
arcgis 官方测距 demo Measurement
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> <title>Measure Tool</title> <link rel="stylesheet" href="https://js.arcgis.com/3.16/esri/themes/calcite/dijit/calcite.css"> <link rel="stylesheet" href="https://js.arcgis.com/3.16/esri/themes/calcite/esri/esri.css"> <style> html, body { height: 100%; width: 100%; margin: 0; } body { background-color: #FFF; overflow: hidden; font-family: "Trebuchet MS"; } #map { border: solid 2px #808775; -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; margin: 5px; padding: 0px; } #titlePane { width: 280px; } </style> <script src="https://js.arcgis.com/3.16/"></script> <script> var map; require([ "dojo/dom", "esri/Color", "dojo/keys", "dojo/parser", "esri/config", "esri/sniff", "esri/map", "esri/units", "esri/SnappingManager", "esri/dijit/Measurement", "esri/layers/FeatureLayer", "esri/renderers/SimpleRenderer", "esri/tasks/GeometryService", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/dijit/Scalebar", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dijit/TitlePane", "dijit/form/CheckBox", "dojo/domReady!" ], function( dom, Color, keys, parser, esriConfig, has, Map, Units, SnappingManager, Measurement, FeatureLayer, SimpleRenderer, GeometryService, SimpleLineSymbol, SimpleFillSymbol ) { parser.parse(); //This sample may require a proxy page to handle communications with the ArcGIS Server services. You will need to //replace the url below with the location of a proxy on your machine. See the 'Using the proxy page' help topic //for details on setting up a proxy page. esriConfig.defaults.io.proxyUrl = "/proxy/"; esriConfig.defaults.io.alwaysUseProxy = false; //This service is for development and testing purposes only. We recommend that you create your own geometry service for use within your applications esriConfig.defaults.geometryService = new GeometryService("https://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); map = new Map("map", { basemap: "satellite", center: [-85.743, 38.256], zoom: 17 }); var sfs = new SimpleFillSymbol( "solid", new SimpleLineSymbol("solid", new Color([195, 176, 23]), 2), null ); var parcelsLayer = new FeatureLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/MapServer/0", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); parcelsLayer.setRenderer(new SimpleRenderer(sfs)); map.addLayers([parcelsLayer]); //dojo.keys.copyKey maps to CTRL on windows and Cmd on Mac., but has wrong code for Chrome on Mac var snapManager = map.enableSnapping({ snapKey: has("mac") ? keys.META : keys.CTRL }); var layerInfos = [{ layer: parcelsLayer }]; snapManager.setLayerInfos(layerInfos); var segmentLength = 0; //used in measure tool to track last segment length var measureLength = 0; // total current length of the line var PrevM = 0; //used in measure tool to track last total measurement var Mtype = ''; //used to track measurement units for display var toolInUse = ''; // used to track which tool is being used var measurement = new Measurement({ map: map, defaultAreaUnit: Units.ACRES, defaultLengthUnit: Units.FEET, }, dom.byId("measurementDiv")); measurement.startup(); var measurementHandler; measurement.on("measure-start", function(evt) { measurementHandler = map.on('click', measureSegment); segmentLength = 0; PrevM = 0; }); measurement.on("measure", function(evt) { measureLength = evt.values; }); measurement.on("measure-end", function(evt) { measurementHandler.remove(); PrevM = 0 }); measurement.on("tool-change", function(evt) { dom.byId('spanSegment').innerHTML = ''; //zero out current values toolInUse = evt.toolName; Mtype = evt.unitName; }); measurement.on("unit-change", function(evt) { Mtype = evt.unitName; }); function measureSegment(e) { if (toolInUse === 'distance') { segmentLength = measureLength - PrevM; //new segment value dom.byId('spanSegment').innerHTML = 'Segment Length: ' + segmentLength.toFixed(1) + ' ' + Mtype; PrevM = measureLength; } } }); </script> </head> <body class="calcite"> <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline',gutters:false" style="width:100%; height:100%;"> <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"> <div style="position:absolute; right:20px; top:10px; z-Index:999;"> <div id="titlePane" data-dojo-type="dijit/TitlePane" data-dojo-props="title:'Measurement', closable:false, open:false"> <div id="measurementDiv"></div> <span id="spanSegment"></span><br> <span style="font-size:smaller;padding:5px 5px;">Press <b>CTRL</b> to enable snapping.</span> </div> </div> </div> </div> </body> </html>
ArcGIS API for JavaScript实现地图双屏联动
ArcGIS API for JavaScript实现地图双屏联动
2014-04-04 10:44 671人阅读 评论(0) 收藏 举报
分类:
arcgis javascript版本地图开发 Dojo(2)
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用ArcGIS API for JavaScript,可以很方便的实现地图联动。最简单的方式是借助Map对象的extent-change 事件,当然,也可以借助zoom-end、pan-end、mouse-drag-end等事件,本文使用第一种方式,实现地图的平移联动、鼠标双击放大联动、点击缩放按钮联动等,具体实现代码如下:
[html] view plain copy print?
<!--
功能:地图分屏
-->
<!-- 功能:地图分屏 --> <!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>地图分屏</title> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/claro/claro.css"> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css"> <link rel="stylesheet" href="css/fenping.css"> <script src="http://js.arcgis.com/3.8/"></script> <script> require([ "dojo/ready", "dojo/parser", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "esri/map", "esri/geometry/Extent", "esri/layers/ArcGISTiledMapServiceLayer", "dojo/domReady!" ], function(ready,parser,BorderContainer, ContentPane,Map,Extent,ArcGISTiledMapServiceLayer) { parser.parse(); /* * 矢量地图的构建 */ var vecMap=new Map("vecMap",{ extent: new Extent({xmin:105,ymin:31,xmax:110,ymax:40, spatialReference:{wkid:4326}}), logo:false }); var tiledMapServiceLayerV=new ArcGISTiledMapServiceLayer("矢量地图服务的url"); vecMap.addLayer(tiledMapServiceLayerV); /* * 影像地图的构建 */ var imgMap=new Map("imgMap",{ extent: new Extent({xmin:105,ymin:31,xmax:110,ymax:40, spatialReference:{wkid:4326}}), logo:false }) var tiledMapServiceLayerI=new ArcGISTiledMapServiceLayer("影像地图的url"); imgMap.addLayer(tiledMapServiceLayerI); /* * 添加extent-change事件,实现地图联动 * flagV矢量地图的extent-change控制标志,flagI影像地图的extent-change控制标志消除死循环情况 */ var flagV=true,flagI=true; vecMap.on("extent-change",function(){ if(flagV){ var vExtent=vecMap.extent; imgMap.setExtent(vExtent); flagI=false; }else if(!flagV){ flagV=true; } }); imgMap.on("extent-change",function(){ if(flagI){ var iExtent=imgMap.extent; vecMap.setExtent(iExtent); flagV=false; }else if(!flagI){ flagI=true; } }); }); </script> </head> <body class="claro"> <div id="mainWindow" data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline'" style="width:100%; height:100%;"> <div id="header" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'top'"> <div id="title">天地图</div> <div id="snippet"> </div> </div> <div id="vecMap" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'left'"> </div> <div id="imgMap" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'"> </div> </div> </body> </html>
页面对应的样式文件为fenping.css,代码如下
html,body { height:100%; width:100%; margin:0; padding:0; } body { background-color:#FFF; color:#333; font-size:14px; overflow:hidden; font-family:Helvetica, "Trebuchet MS", Arial, sans-serif; } #header { border:solid 1px #224a54; height:50px; margin:2px; background-image: url("../images/background.jpg"); } #title { color:white; font-size:30px; text-align:left; font-weight:bolder; padding-top:10px; padding-left:15px; } #snippet { font-size:14px; color:#444; padding-left:15px; } #vecMap { border:solid 1px #b0c4de; width:48%; margin:2px; } #imgMap { border:solid 1px #b0c4de; width:48%; margin:2px; } .dj_ie .infowindow .window .top .right .user .content { position: relative; } .dj_ie .simpleInfoWindow .content {position: relative;}
arcgis jsapi 上一视图下一视图 Navigation可以实现切换视图
arcgis Navigation可以实现切换视图 切换到视图 zoomToNextExtent() None Zoom to next extent in extent history. zoomToPrevExtent() None Zoom to previous extent in extent history.
Y坐标: " + geoPt.x.toFixed(4)
"
" +
" +
" +
Y坐标: " + geoPt.x.toFixed(4)
"
标签: javascript
|
分类: JsAPI
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Find Address</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/ tundra/tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script> <script type="text/javascript"> dojo.require("esri.map"); dojo.require("esri.tasks.locator"); function init() { var map = new esri.Map("map", { extent: new esri.geometry.Extent (-95.2991, 38.9379, -95.2476, 38.963, new esri.SpatialReference({wkid:4326})) }); var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer ("http://server.arcgisonline.com/ArcGIS/rest/services/ ESRI_StreetMap _World_2D/MapServer"); map.addLayer(tiledMapServiceLayer); var locator = new esri.tasks.Locator ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/ Locators/ESRI_Geocode_USA/GeocodeServer"); var infoTemplate = new esri.InfoTemplate("Location", "Street: ${Address} <br />City: ${City}<br />State: ${State}<br />Zip: ${Zip}"); var symbol = new esri.symbol.SimpleMarkerSymbol (esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 15, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,255]), 2), new dojo.Color([0,0,255])); dojo.connect(locator, "onLocationToAddressComplete", function(candidate) { if (candidate.address) { var graphic = new esri.Graphic(candidate.location, symbol, candidate.address, infoTemplate); map.graphics.add(graphic); map.infoWindow.setTitle(graphic.getTitle()); map.infoWindow.setContent(graphic.getContent()); var screenPnt = map.toScreen(candidate.location); map.infoWindow.show(screenPnt,map.getInfoWindowAnchor(screenPnt)); } }); dojo.connect(map, "onClick", function(evt) { map.graphics.clear(); locator.locationToAddress(evt.mapPoint, 100); }); } dojo.addOnLoad(init); </script> </head> <body class="tundra"> Left click on the map to get address. <div id="map" style="width:1200px; height:600px; border:1px solid #000;"></div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Find Task</title> <style type="text/css"> @import "http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/ themes/tundra/tundra.css"; @import "http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dojox/ grid/resources/Grid.css"; body { font-size: 0.9em; font-family: Geneva, Arial, Helvetica, sans-serif; } .heading { font-weight: bold; padding-bottom: 0.25em; } #grid { border: 1px solid #333; width: 33em; height: 30em; } </style> <script type="text/javascript">djConfig={parseOnLoad:true}</script> <script type="text/javascript" src="http://serverapi.arcgisonline.com//jsapi/arcgis/?v=1.6"></script> <script type="text/javascript" language="Javascript"> dojo.require("dojox.grid.DataGrid"); dojo.require("dojo.data.ItemFileReadStore"); dojo.require("esri.map"); dojo.require("esri.tasks.find"); var findTask, findParams, map; function init() { map = new esri.Map("map"); var censusMapLayer = new esri.layers.ArcGISDynamicMapServiceLayer ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/ Specialty/ESRI_StatesCitiesRivers_USA/MapServer",{id:"usa"}); map.addLayer(censusMapLayer); //创建url找到目标地图服务 findTask = new esri.tasks.FindTask ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/ Specialty/ESRI_StatesCitiesRivers_USA/MapServer"); //创建查找参数和定义已知值 findParams = new esri.tasks.FindParameters(); findParams.returnGeometry = true; findParams.layerIds = [0,1,2]; findParams.searchFields = ["CITY_NAME","NAME","SYSTEM","STATE_ABBR","STATE_NAME"]; } function execute(searchText) { //设置搜索要查找的文本参数 findParams.searchText = searchText; findTask.execute(findParams,showResults); } function showResults(results) { //图形标注 var markerSymbol = new esri.symbol.SimpleMarkerSymbol (esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 1), new dojo.Color([0,255,0,0.25])); var lineSymbol = new esri.symbol.SimpleLineSymbol (esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([255,0,0]), 1); var polygonSymbol = new esri.symbol.SimpleFillSymbol (esri.symbol.SimpleFillSymbol.STYLE_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.25])); //查找结果返回findResult集合. map.graphics.clear(); var dataForGrid = []; //建立一个集合用于添加地图中找到的图形 dojo.forEach(results,function(result){ var graphic = result.feature; dataForGrid.push([result.layerName,result.foundFieldName,result.value]); switch (graphic.geometry.type) { case "point": graphic.setSymbol(markerSymbol); break; case "polyline": graphic.setSymbol(lineSymbol); break; case "polygon": graphic.setSymbol(polygonSymbol); break; } map.graphics.add(graphic); }); var data={ items:dataForGrid }; var store = new dojo.data.ItemFileReadStore({data:data}); grid.setStore(store); } dojo.addOnLoad(init); </script> </head> <body class="tundra"> <div class="heading">Find State/City/River: <input type="text" id="searchText" value="KS" /> <input type="button" value="Find" onclick="execute(dojo.byId('searchText').value);" /></div> <div id="map" style="width:400px; height:300px; border:1px solid #000;"></div> <!--对照外地位置ID用于显示没有字段的数据--> <table dojotype="dojox.grid.DataGrid" jsid="grid" id="grid" style="width:400px;"> <thead> <tr> <th field="0" width="125px">Layer Name</th> <th field="1" width="125px">Field Name</th> <th field="2" width="100%">Field Value</th> </tr> </thead> </table> </div> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Dynamic Map Service Layer with Custom Projection and Extent</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo /dijit /themes /tundra /tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"> </script> <script type="text/javascript"> dojo.require("esri.map"); function init() { var customExtentAndSR = new esri.geometry.Extent(-20037506,- 9823074,20037506,10214431,new esri.SpatialReference({"wkid":54052})); //创建新的自定义空间参考和范围地图,该地图将在Goode Homolosina土地投影 var map = new esri.Map("map", {extent:customExtentAndSR}); //地图服务层有一个4325的默认空间参考 var mapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/ Demographics /ESRI_ Population_World/MapServer"); map.addLayer(mapServiceLayer); } dojo.addOnLoad(init); </script> </head> <body> <div id="map" class="tundra" style="position:relative; width:1024px; height:512px; border:2px solid #000;background-color:#99CCFF;"></div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>QueryTask with geometry, results as an InfoWindow</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit /themes/tundra/tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi /arcgis/?v=1.6"></script> <script type="text/javascript" language="Javascript"> dojo.require("esri.map"); dojo.require("esri.tasks.query"); var map, queryTask, query; var symbol, infoTemplate; function init() { //创建地图 map = new esri.Map("mapDiv"); //创建并添加层 var layer = new esri.layers.ArcGISDynamicMapServiceLayer ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services /Specialty/ESRI_StateCityHighway_USA/MapServer"); map.addLayer(layer); //监听click事件,当用户点击地图时执行executeQueryTask方法 dojo.connect(map, "onClick", executeQueryTask); //建立查询任务 queryTask = new esri.tasks.QueryTask ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services /Specialty/ESRI_StateCityHighway_USA/MapServer/1"); //监听onComplete事件的处理结果,使用queryTask.execute方法回调 //dojo.connect(queryTask, "onComplete", showResults); //建立查询过滤器 query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["STATE_NAME", "STATE_FIPS", "STATE_ABBR", "HYPERLINK", "AREA"]; //在信息窗口创建infoTemplate //${属性名}将取代目前的功能和属性值. infoTemplate = new esri.InfoTemplate("${STATE_NAME}", "State Fips: ${STATE_FIPS}<br />Abbreviation: ${STATE_ABBR}<br />Area: ${AREA}"); symbol = new esri.symbol.SimpleFillSymbol (esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.5])); } function executeQueryTask(evt) { //用户点击onClick事件返回地图上EVT点. //包含在MapPoint(esri.geometry.point)和screenPoint(pixel像素点). //设置查询几何等于evt.mapPoint query.geometry = evt.mapPoint; //执行任务和完成showResults queryTask.execute(query, showResults); } function showResults(featureSet) { //删除地图上所有的图形层 map.graphics.clear(); var features = featureSet.features; //QueryTask返回featureSet类型.通过featureSet的循环把他们添加到信息窗口 for (var i=0, il=features.length; i<il; i++) { //从featureSet中得到当前实例. //从当前实例赋值给graphic var graphic = features[0]; graphic.setSymbol(symbol); //设置图形的infoTemplate. graphic.setInfoTemplate(infoTemplate); //添加当前这个图形到地图图层中 map.graphics.add(graphic); } dojo.connect(map.graphics, "onMouseMove", function(evt) { var g = evt.graphic; map.infoWindow.setContent(g.getContent()); map.infoWindow.setTitle(g.getTitle()); map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor (evt.screenPoint)); }); dojo.connect(map.graphics, "onMouseOut", function() { map.infoWindow.hide(); } ); } dojo.addOnLoad(init); </script> </head> <body class="tundra"> Click on a state to get more info. When state is highlighted, move mouse over state to get more info. <div id="mapDiv" style="width:600px; height:600px; border:1px solid #000;"></div> </body> </html>
标签:
|
分类:
JsAPI |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Create Map</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script> <script type="text/javascript"> dojo.require("esri.map"); function init() { var map = new esri.Map("map"); var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"); map.addLayer(tiledMapServiceLayer); dojo.connect(map, "onExtentChange", showExtent); } function showExtent(extent) { var s = ""; s = "XMin: "+ extent.xmin + " " +"YMin: " + extent.ymin + " " +"XMax: " + extent.xmax + " " +"YMax: " + extent.ymax; dojo.byId("info").innerHTML = s; } dojo.addOnLoad(init); </script> </head> <body class="tundra"> <div id="map" style="width:900px; height:600px; border:1px solid #000;"></div> <div id="info" style="padding:5px; margin:5px; background-color:#eee;"></div> Creates a map and adds an ArcGISTiledMapServiceLayer. On map onExtentChanged event you should see the extent printed below the map.<br /> Map navigation using mouse: <ul> <li>Drag to pan</li> <li>SHIFT + Click to recenter</li> <li>SHIFT + Drag to zoom in</li> <li>SHIFT + CTRL + Drag to zoom out</li> <li>Mouse Scroll Forward to zoom in</li> <li>Mouse Scroll Backward to zoom out</li> <li>Use Arrow keys to pan</li> <li>+ key to zoom in a level</li> <li>- key to zoom out a level</li> <li>Double Click to Center and Zoom in</li> </ul> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Create Map Display Mouse Coordinates</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js /dojo/dijit/theme /tundra /tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script> <script type="text/javascript"> dojo.require("esri.map"); function init() { var map = new esri.Map("map"); dojo.connect(map, "onLoad", function() { //地图加载后,监听到移动鼠标拖动事件 dojo.connect(map, "onMouseMove", showCoordinates); dojo.connect(map, "onMouseDrag", showCoordinates); }); var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"); map.addLayer(tiledMapServiceLayer); } function showCoordinates(evt) { //从事件中得到mapPoint var mp = evt.mapPoint; //显示鼠标坐标 dojo.byId("info").innerHTML = mp.x + ", " + mp.y; } dojo.addOnLoad(init); </script> </head> <body class="tundra"> <div id="map" style="position:relative; width:900px; height:600px; border:1px solid #000;"> <span id="info" style="position:absolute; right:25px; bottom:5px; color:#000; z-index:50;"></span> </div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>ArcGIS JavaScript API Map Layer</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra /tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis /?v=1.6"></script> <script type="text/javascript"> dojo.require("esri.map"); var myMap, portlandParcels, transportation, census; function init() { myMap = new esri.Map("mapDiv"); portlandParcels = new esri.layers.ArcGISTiledMapServiceLayer ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Portland/Portland_ ESRI_LandBase_AGO/MapServer"); myMap.addLayer(portlandParcels); transportation = new esri.layers.ArcGISTiledMapServiceLayer ("http://server.arcgisonline.com/ArcGIS/rest/services/Reference/ESRI_Transportation _World_2D/MapServer", {"id":"agoreference", "opacity":0.5}); myMap.addLayer(transportation); census = new esri.layers.ArcGISDynamicMapServiceLayer ("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census _USA/MapServer", {"id":"census","opacity":0.5}); myMap.addLayer(census); } function getMapLayers() { for (var j=0, jl=myMap.layerIds.length; j<jl; j++) { var currentLayer = myMap.getLayer(myMap.layerIds[j]); alert("id: " + currentLayer.id + ", visible: " + currentLayer.visible + ", opacity: " + currentLayer.opacity); } } function getSr() { alert("Spatial Reference WKID: " + myMap.spatialReference.wkid); } function getScales() { var tileInfo = portlandParcels.tileInfo; var scales = ""; for (var j=0, jl=tileInfo.lods.length; j<jl; j++) { var level = tileInfo.lods[j].level; var scale = tileInfo.lods[j].scale; var resolution = tileInfo.lods[j].resolution; scales = scales + "level: " + level + ", scale: " + scale + ", resolution: " + resolution + "\n"; } alert(scales); } function getMapWidthHeight() { alert("Width = " + myMap.width + "; Height = " + myMap.height); } dojo.addOnLoad(init); </script> </head> <body class="tundra"> <div id="mapDiv" style="width:900px; height:600px; border:1px solid #000;"></div> Simple example illustrating working with map and layer properties<br /> <input type="button" value="所有图层" onclick="getMapLayers()" /> <input type="button" value="空间参考" onclick="getSr()" /> <input type="button" value="比例级别" onclick="getScales()" /> <input type="button" value="地图的宽度高度" onclick="getMapWidthHeight()" /> <br /> </body> </html>
- //量测
- function measutreLength() {
- toolbar.activate(esri.toolbars.Draw.POLYLINE);
- }
- function measutreArea() {
- toolbar.activate(esri.toolbars.Draw.POLYGON);
- }
//量测 function measutreLength() { toolbar.activate(esri.toolbars.Draw.POLYLINE); } function measutreArea() { toolbar.activate(esri.toolbars.Draw.POLYGON); } [csharp] view plain copy //量算 function doMeasure(geometry) { //更加类型设置显示样式 measuregeometry = geometry; toolbar.deactivate(); switch (geometry.type) { case "polyline": var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0]), 2); break; case "polygon": var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25])); break; } //设置样式 var graphic = new esri.Graphic(geometry,symbol); //清除上一次的画图内容 myMap.graphics.clear(); myMap.graphics.add(graphic); //进行投影转换,完成后调用projectComplete MeasureGeometry(geometry); } //投影转换完成后调用方法 function MeasureGeometry(geometry) { //如果为线类型就进行lengths距离测算 if (geometry.type == "polyline") { var lengthParams = new esri.tasks.LengthsParameters(); lengthParams.polylines = [geometry]; lengthParams.lengthUnit = esri.tasks.GeometryService.UNIT_METER; lengthParams.geodesic = true; lengthParams.polylines[0].spatialReference = new esri.SpatialReference(4326); geometryService.lengths(lengthParams); dojo.connect(geometryService, "onLengthsComplete", outputDistance); } //如果为面类型需要先进行simplify操作在进行面积测算 else if (geometry.type == "polygon") { var areasAndLengthParams = new esri.tasks.AreasAndLengthsParameters(); areasAndLengthParams.lengthUnit = esri.tasks.GeometryService.UNIT_METER; areasAndLengthParams.areaUnit = esri.tasks.GeometryService.UNIT_SQUARE_METERS; this.outSR = new esri.SpatialReference({ wkid: 102113 }); geometryService.project([geometry], this.outSR, function (geometry) { geometryService.simplify(geometry, function (simplifiedGeometries) { areasAndLengthParams.polygons = simplifiedGeometries; areasAndLengthParams.polygons[0].spatialReference = new esri.SpatialReference(102113); geometryService.areasAndLengths(areasAndLengthParams); }); }); dojo.connect(geometryService, "onAreasAndLengthsComplete", outputAreaAndLength); } } //显示测量距离 function outputDistance(result) { var CurX = measuregeometry.paths[0][measuregeometry.paths[0].length - 1][0]; var CurY = measuregeometry.paths[0][measuregeometry.paths[0].length - 1][1]; var CurPos = new esri.geometry.Point(CurX, CurY, myMap.spatialReference); myMap.infoWindow.setTitle("距离测量"); myMap.infoWindow.setContent(" 测 量 长 度 : <strong>" + parseInt(String(result.lengths[0])) + "米</strong>"); myMap.infoWindow.show(CurPos); } //显示测量面积 function outputAreaAndLength(result) { var CurX = (measuregeometry._extent.xmax + measuregeometry._extent.xmin) / 2; var CurY = (measuregeometry._extent.ymax + measuregeometry._extent.ymin) / 2 var CurPos = new esri.geometry.Point(CurX, CurY, myMap.spatialReference); myMap.infoWindow.setTitle("面积测量"); myMap.infoWindow.setContent(" 面积 : <strong>" + parseInt(String(result.areas[0])) + "平方米</strong> 周长:" + parseInt(String(result.lengths[0])) + "米"); myMap.infoWindow.show(CurPos); //alert("面积:" + dojo.number.format(result.areas[0]) + "平方米" + " 长度:" + dojo.number.format(result.lengths[0]) + "米"); }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Set Map Extent Using Second Service</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit /themes /tundra /tundra.css"> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/ ?v=1.6"></script> <script type="text/javascript"> dojo.require("esri.map"); var myMap, myService1, myService2; function initLayers() { var primaryMapServiceURL = "http://server.arcgisonline.com /ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"; myService1 = new esri.layers.ArcGISTiledMapServiceLayer (primaryMapServiceURL); var secondaryMapServiceURL = "http://sampleserver1.arcgisonline.com /ArcGIS/rest/services/Petroleum/KGS_OilGasFields_Kansas/MapServer"; myService2 = new esri.layers.ArcGISDynamicMapServiceLayer (secondaryMapServiceURL, {opacity:0.75}); var layerLoadCount = 0; //当两个层加载时运行createMapAddLayers if (myService1.loaded) { layerLoadCount += 1; if (layerLoadCount === 2) { createMapAddLayers(myService1,myService2); } } else { dojo.connect(myService1, "onLoad", function(service) { layerLoadCount += 1; if (layerLoadCount === 2) { createMapAddLayers(myService1,myService2); } }); } if (myService2.loaded) { layerLoadCount += 1; if (layerLoadCount === 2) { createMapAddLayers(myService1,myService2); } } else { dojo.connect(myService2, "onLoad", function(service) { layerLoadCount += 1; if (layerLoadCount === 2) { createMapAddLayers(myService1,myService2); } }); } } //创建一个地图,设置范围并添加到地图服务中 function createMapAddLayers(myService1,myService2) { //创建地图 myMap = new esri.Map("mapDiv", { extent:myService2.fullExtent }); myMap.addLayer(myService1); myMap.addLayer(myService2); } dojo.addOnLoad(initLayers); </script> </head> <body class="tundra"> <div id="mapDiv" style="width:600px; height:400px; border:1px solid #000;"></div> <br/> This map shows two services: <ul> <li>An ArcGIS Online tiled service that has a world extent.</li> <li>A second dynamic service with an extent of the State of Kansas. This is the extent used when the maps are first displayed. </li> </ul> Note that if you want to combine to tiled services in the same map, they must have the same tile configuration. </body> </html>
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> <title>Context Menu for Map and Graphics</title> <link rel="stylesheet" href= "http://js.arcgis.com/3.14/dijit/themes/claro/claro.css"> <link rel="stylesheet" href="http://js.arcgis.com/3.14/esri/css/esri.css"> <style> html, body { height:100%; margin: 0; padding: 0; width:100%; overflow:hidden; } #header { overflow:hidden; height: 1.6em; } div.insetType { color: #97F900; font-size: 24px; font-family: Rockwell, Georgia, "Times New Roman", Times, serif; padding-left: 25px; } </style> <script src="http://js.arcgis.com/3.14/"></script> <script> var map, editToolbar, ctxMenuForGraphics, ctxMenuForMap; var selected, currentLocation; require([ "esri/map", "esri/geometry/Point", "esri/geometry/Polygon", "esri/toolbars/draw", "esri/toolbars/edit", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/graphic", "esri/geometry/jsonUtils", "esri/Color", "dojo/parser", "dijit/Menu", "dijit/MenuItem", "dijit/MenuSeparator", "dijit/form/Button", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!" ], function( Map, Point, Polygon, Draw, Edit, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, Graphic, geometryJsonUtils, Color, parser, Menu, MenuItem, MenuSeparator ) { parser.parse(); map = new Map("map", { basemap: "satellite", center: [20.039, 62.739], zoom: 3 }); map.on("load", createToolbarAndContextMenu); function createToolbarAndContextMenu() { // Add some graphics to the map addGraphics(); // Create and setup editing tools editToolbar = new Edit(map); map.on("click", function(evt) { editToolbar.deactivate(); }); createMapMenu(); createGraphicsMenu(); } function createMapMenu() { // Creates right-click context menu for map ctxMenuForMap = new Menu({ onOpen: function(box) { // Lets calculate the map coordinates where user right clicked. // We'll use this to create the graphic when the user clicks // on the menu item to "Add Point" currentLocation = getMapPointFromMenuPosition(box); editToolbar.deactivate(); } }); ctxMenuForMap.addChild(new MenuItem({ label: "Add Point", onClick: function(evt) { var symbol = new SimpleMarkerSymbol( SimpleMarkerSymbol.STYLE_SQUARE, 30, new SimpleLineSymbol( SimpleLineSymbol.STYLE_SOLID, new Color([200,235, 254, 0.9]), 2 ), new Color([200, 235, 254, 0.5])); var graphic = new Graphic(geometryJsonUtils.fromJson(currentLocation.toJson()), symbol); map.graphics.add(graphic); } })); ctxMenuForMap.startup(); ctxMenuForMap.bindDomNode(map.container); } function createGraphicsMenu() { // Creates right-click context menu for GRAPHICS ctxMenuForGraphics = new Menu({}); ctxMenuForGraphics.addChild(new MenuItem({ label: "Edit", onClick: function() { if ( selected.geometry.type !== "point" ) { editToolbar.activate(Edit.EDIT_VERTICES, selected); } else { alert("Not implemented"); } } })); ctxMenuForGraphics.addChild(new MenuItem({ label: "Move", onClick: function() { editToolbar.activate(Edit.MOVE, selected); } })); ctxMenuForGraphics.addChild(new MenuItem({ label: "Rotate/Scale", onClick: function() { if ( selected.geometry.type !== "point" ) { editToolbar.activate(Edit.ROTATE | Edit.SCALE, selected); } else { alert("Not implemented"); } } })); ctxMenuForGraphics.addChild(new MenuItem({ label: "Style", onClick: function() { alert("Not implemented"); } })); ctxMenuForGraphics.addChild(new MenuSeparator()); ctxMenuForGraphics.addChild(new MenuItem({ label: "Delete", onClick: function() { map.graphics.remove(selected); } })); ctxMenuForGraphics.startup(); map.graphics.on("mouse-over", function(evt) { // We'll use this "selected" graphic to enable editing tools // on this graphic when the user click on one of the tools // listed in the menu. selected = evt.graphic; // Let's bind to the graphic underneath the mouse cursor ctxMenuForGraphics.bindDomNode(evt.graphic.getDojoShape().getNode()); }); map.graphics.on("mouse-out", function(evt) { ctxMenuForGraphics.unBindDomNode(evt.graphic.getDojoShape().getNode()); }); } // Helper Methods function getMapPointFromMenuPosition(box) { var x = box.x, y = box.y; switch( box.corner ) { case "TR": x += box.w; break; case "BL": y += box.h; break; case "BR": x += box.w; y += box.h; break; } var screenPoint = new Point(x - map.position.x, y - map.position.y); return map.toMap(screenPoint); } function addGraphics() { // Adds pre-defined geometries to map var polygonSymbol = new SimpleFillSymbol( SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol( SimpleLineSymbol.STYLE_DOT, new Color([151, 249, 0, 0.8]), 3 ), new Color([151, 249, 0, 0.45]) ); var polygon = new Polygon({ "rings": [ [ [-4226661.916056009, 8496372.808143634], [-3835304.3312360067, 8731187.359035634], [-2269873.991956003, 9005137.668409634], [-1213208.5129420012, 8613780.083589634], [-1017529.7205320001, 8065879.464841632], [-1213208.5129420012, 7478843.087611631], [-2230738.233474003, 6891806.710381631], [-2935181.8861500043, 6735263.6764536295], [-3522218.263380006, 6891806.710381631], [-3952711.606682008, 7165757.01975563], [-4265797.674538009, 7283164.295201631], [-4304933.433020009, 7635386.121539632], [-4304933.433020009, 7674521.880021632], [-4226661.916056009, 8496372.808143634] ] ], "spatialReference": { "wkid": 102100 } }); var arrow = new Polygon({ "rings": [ [ [9862211.137464028, 6617856.40100763], [8922952.933896024, 5522055.163511626], [8922952.933896024, 5991684.265295628], [6105178.323192019, 5991684.265295628], [6105178.323192019, 7087485.50279163], [8922952.933896024, 7087485.50279163], [8922952.933896024, 7557114.604575632], [9862211.137464028, 6617856.40100763] ] ], "spatialReference": { "wkid": 102100 } }); var triangle = new Polygon({ "rings": [ [ [2426417.02588401, 8535508.566625634], [4304933.433020014, 12292541.380897645], [6183449.840156019, 8535508.566625634], [2426417.02588401, 8535508.566625634] ] ], "spatialReference": { "wkid": 102100 } }); map.graphics.add(new Graphic(polygon, polygonSymbol)); map.graphics.add(new Graphic(arrow, polygonSymbol)); map.graphics.add(new Graphic(triangle, polygonSymbol)); } }); </script> </head> <body class="claro" style="font-size: 0.75em;"> <div data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="width:100%;height:100%;"> <div id="header" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'"> <div class="insetType">Right-click on map or graphic to view context menu</div> </div> <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" ></div> </div> </body> </html>
该例展示了加载基础底图的方法。例子中采用ArcGIS Online中的缓存服务,地图范围设置为San Francisco,控制代码如下:
var initExtent= newesri.geometry.Extent({"xmin":-13635568.034589134,"ymin":4541606.359162286,"xmax":-13625430.573712826,"ymax":4547310.472398059,"spatialReference":{"wkid":102100}});
map = new esri.Map("map",{extent:initExtent});
如果想更改初始地图显示范围,可利用浏览器的开发者工具获取范围,以IE 9为例,步骤如下:
运行该应用程序
将地图缩放到理想范围
按F2,在如下图弹出的界面中切换到控制台标签,
输入dojo.toJson(map.extent.toJson());
复制该段代码,替换initExtent 中相应内容。
例子代码如下:
[html] view plaincopy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /> <!--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>Topographic Map</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.8/js/dojo/dijit/themes/claro/claro.css"> <style> html, body { height: 100%; width: 100%; margin: 0; padding: 0; } .esriScalebar{ padding: 20px 20px; } #map{ padding:0; } </style> <script type="text/javascript">var djConfig = {parseOnLoad: true};</script> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.8"></script> <script type="text/javascript"> dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("esri.map"); var map; function init() { var initExtent = new esri.geometry.Extent({"xmin":-122.46,"ymin":37.73,"xmax":-122.36,"ymax":37.77,"spatialReference":{"wkid":4326}}); map = new esri.Map("map",{ extent:esri.geometry.geographicToWebMercator(initExtent) }); //Add the topographic layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"); map.addLayer(basemap); dojo.connect(map, 'onLoad', function(theMap) { //resize the map when the browser resizes dojo.connect(dijit.byId('map'), 'resize', map,map.resize); }); } dojo.addOnLoad(init); </script> </head> <body class="claro"> <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" style="width: 100%; height: 100%; margin: 0;"> <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="border:1px solid #000;padding:0;"> </div> </div> </body> </html>