🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
基于MapView创建2D地图实例 官方示例:https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=intro-mapview ## 一、mapView示例 ~~~ require([ 'esri/Map', 'esri/views/MapView', 'dojo/domReady!' ], function(Map, MapView) { var map = new Map({ basemap: 'streets' // dark-gray }); var mapView = new MapView({ map: map, container: 'js_map', center: [102.9331224074, 25.1049040686], zoom: 6, // rotation: -127.7 // 旋转地图 }); }); ~~~ ![](https://box.kancloud.cn/9dbefbd3258a5ee0145d2d3d64a37b89_529x443.jpg) **注意:dojo/domReady!一定要放在最后引入** ## 二、实例创建完成回调 ~~~ mapView.when(function(){ // This function will execute once the promise is resolved }, function(error){ // This function will execute if the promise is rejected due to an error }); ~~~ ## 三、属性设置 ~~~ mapView.set({ center: [121.3022630323934, 29.868660680345652], zoom: 10 }); ~~~ ## 四、观察属性变动 ~~~ mapView.watch(['zoom'], function(newV, oldV, propName) { console.log(newV, oldV, propName) }); ~~~ ## 五、常用事件 ~~~ // 点击事件 mapView.on('click', function(res) { var mapPoint = res.mapPoint, zoom = mapView.zoom; console.log([mapPoint.longitude, mapPoint.latitude], zoom) }); // 缩放事件 mapView.on('mouse-wheel', function(res) { console.log(res); }); // 拖拽事件 mapView.on('drag', function(res) { console.log(res); }); // 大小改变事件 mapView.on('resize', function(res) { console.log(res); }); // 图层创建呈现完成事件 mapView.on('layerview-create', function(res) { console.log(res); }); mapView.on('pointer-up', function(res) { if(res.button === 2) { // 右健 var divEle = document.createElement('div'); divEle.style.cssText = 'position: fixed;left: '+res.x+'px;top: '+res.y+'px;background-color: #ddd;border: 1px solid #ccc;'; divEle.textContent = 'Hello ArcGis.'; document.body.appendChild(divEle); } }); ~~~ ## 六、事件示例 1、通过mapView绑定 ``` // 点标记 var pointGraphic = new Graphic({ attributes: { id: 'aaa' }, geometry: { type: "point", longitude: 102.91835013921454, latitude: 24.07329419928656 }, symbol: { type: "simple-marker", color: [226, 119, 40], outline: { color: [255, 255, 255], width: 2 } } }); // 绑定事件 mapView.on('click', function(event) { mapView.hitTest(event).then(function(res) { var marker = res.results[0]; console.log(marker.graphic.attributes) // 获取捆绑的属性 }); }); ``` 2、通过body绑定(适合文本标记) ``` $('body').on('click', 'text',function(e) { e.stopPropagation(); console.log($(this).text()) }); ```