看本文前最好對第二章(Mapping and Views)中的Map和View類有了解。
視圖類有一個屬性是Popup類型的popup,查閱API知道這個就是視圖的彈窗,每一個View的執行個體都有一個popup。
這個popup屬性在View對象執行個體化的時候就執行個體化了的,即随着View的出生,它也會出生,它擁有預設的樣子,它顯示的文字也是預設的樣式。
我們看看Popup這個類:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuQTM1MTN0kTNx0yN0gDN0YTNzIDNxIDM3EDMy0CN3AzN5ATMvwlMwcTMwIzLcRzNwcTOwEzLcd2bsJ2Lc12bj5ycn9Gbi52YuUTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
直接繼承自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>
源代碼