看本文前最好對第二章(Mapping and Views)中的Map和View類有理解。
視圖類有一個屬性是Popup類型的popup,查閱API知道這個就是視圖的彈窗,每一個View的實例都有一個popup。
這個popup屬性在View對象實例化的時候就實例化了的,即隨着View的出生,它也會出生,它擁有默認的樣子,它顯示的文字也是默認的樣式。
我們看看Popup這個類:
直接繼承自Accessor,位於widgets模塊下,說明Popup(彈窗)也是小部件的一種。但是為什么要單獨拿出來講呢?可能用法上比較復雜吧。
如果用戶對彈窗有更高的樣式要求,官方的說法是
可以自己new一個Popup對象,替換掉view默認的popup即可。
其實Popup有個兄弟類,叫PopupTemplate,它長得很像Popup,但是在功能上更服務於Layer和Graphic,即地理數據,而且也是高度可自定義的,在下一節會細說這兩個的區別。
說完了Popup這個新玩意兒,我就來說說第一個例子吧!
它的功能就是點擊View上的一個地方,就會彈出一個小窗,顯示經緯度和其他信息。
引用
直接給:
require( [ "esri/tasks/Locator", "esri/Map", "esri/views/MapView", "dojo/domReady!" ], function(){} );
函數參數中因為Map和View 對象是經常性出現的,所以就用 var map = new Map(...);代替了(以后也是)
所以函數參數的關鍵代碼是:
function(Locator, Map, MapView){ var map = new Map(...); var view = new MapView(...); var locatorTask = new Locator({ url : "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer";
}); view.on("click", funcion(event){...}) }
locatorTask是一個Locator類的對象,用於定位服務。這個不用知道太詳細,因為本例中重點並不是它。
重點應該是view的事件“click”,寫法同上,但是事件的方法體沒有寫完整。
我們就來看看這個事件的方法體具體是什么:
function(event){ //獲取經緯度 var lat = Math.round(event.mapPoint.latitude * 1000) / 1000; var lon = Math.round(event.mapPoint.longitude * 1000) / 1000; view.popup.open({ title: "Reverse geocode: [" + lon + ", " + lat + "]", location: event.mapPoint }); //定位服務,可以省略,不是本例中的主要內容 locatorTask.locationToAddress(event.mapPoint) .then(function(response) { var address = response.address.Match_addr; view.popup.content = address; }) .otherwise(function(err) { view.popup.content = "No address was found for this location"; }); }
我們看到了,從click事件的event參數中獲取到了lat和lon,即經緯度,用Math.round方法對數字進行了一定的處理。
然后關鍵的一句:
view.popup.open({...});
我們先不說這個是什么,但是肯定是一個方法。
下面的then()方法,是Locator對象的locationToAddress方法的Promise返回對象的回調,用於獲取地址成功后把地址顯示到popup上。
關鍵的一句:
view.popup.content = address;
在下面otherwise()方法也有類似的。
現在,我們轉到Popup這個類的定義。
Popup類
繼承自Accessor類
主要屬性:actions(Collection類型)、content(String或Node類型)、location(Point類型)、title(String類型)...
主要方法:open()、destroy()...
看得出,上面的代碼使用了open方法,content屬性、location屬性、title屬性。
open方法會把popup的visible屬性改為true,然后顯示到指定的位置:location。
是不是很簡單呢?
更深一層的理解,既然能open,那么肯定是有這個實例的,更說明了popup這個屬性是一個對象,在View實例化的時候就完成了實例化。
對其content、title屬性進行設置就可以在彈出窗中看到想要的內容和標題了。
總結一下。
1. View對象自帶Popup實例,並隨着View對象實例化而實例化。
2. Popup使用open()方法顯示出來,接受location、title、content等可選參數以指定內容、彈窗點等。
最后給個圖:
很顯而易見。
給出官方的源代碼(沒有刪除注釋)

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"> <title>Get started with popups - 4.2</title> <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } #instruction { z-index: 99; position: absolute; top: 15px; left: 50%; padding: 5px; margin-left: -175px; height: 20px; width: 350px; background: rgba(25, 25, 25, 0.8); color: white; } </style> <link rel="stylesheet" href="https://js.arcgis.com/4.2/esri/css/main.css"> <script src="https://js.arcgis.com/4.2/"></script> <script> require([ "esri/tasks/Locator", "esri/Map", "esri/views/MapView", "dojo/domReady!" ], function( Locator, Map, MapView ) { // Set up a locator task using the world geocoding service var locatorTask = new Locator({ url: "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer" }); // Create the Map var map = new Map({ basemap: "streets-navigation-vector" }); // Create the MapView var view = new MapView({ container: "viewDiv", map: map, center: [-116.3031, 43.6088], zoom: 12 }); /******************************************************************* * This click event sets generic content on the popup not tied to * a layer, graphic, or popupTemplate. The location of the point is * used as input to a reverse geocode method and the resulting * address is printed to the popup content. *******************************************************************/ view.on("click", function(event) { // Get the coordinates of the click on the view var lat = Math.round(event.mapPoint.latitude * 1000) / 1000; var lon = Math.round(event.mapPoint.longitude * 1000) / 1000; view.popup.open({ // Set the popup's title to the coordinates of the location title: "Reverse geocode: [" + lon + ", " + lat + "]", location: event.mapPoint // Set the location of the popup to the clicked location }); // Display the popup // Execute a reverse geocode using the clicked location locatorTask.locationToAddress(event.mapPoint).then(function( response) { // If an address is successfully found, print it to the popup's content var address = response.address.Match_addr; view.popup.content = address; }).otherwise(function(err) { // If the promise fails and no result is found, print a generic message // to the popup's content view.popup.content = "No address was found for this location"; }); }); }); </script> </head> <body> <div id="viewDiv"></div> <div id="instruction">Click any location on the map to see its street address</div> </body> </html>