让我们从引用(https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#open)中考虑这段代码:
view.on("click", function(evt){
view.popup.open({
location: evt.mapPoint, // location of the click on the view
title: "Some title",
});
这很有效。但是如何打开一个弹出窗口,由预定义的lng,lat coords指定?
首先尝试:
var point = new Point({latitude:lat,longitude:lng});
view.popup.open({
location: point,
title: "Some title"
});
这不起作用。原因是创建的点当前与地图视图断开连接。有没有办法通过指定(lng,lat)接收当前视图的屏幕坐标(x,y)?在google maps api中有像latLngToDivPixel,latLngToDivPoint这样的方法,那么argis为这个任务提供了什么?
答案 0 :(得分:1)
上面的答案/问题对于版本4.0和4.1是正确的。
但是,对于新发布的版本4.2,这已经简化了。当地图在web mercator投影中时,Popup.location现在自动将WGS84(纬度,经度)点转换为web mercator。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="description" content="[Define custom Popup actions - 4.2]">
<!--
ArcGIS API for JavaScript, https://js.arcgis.com
For more information about the popup-actions sample, read the original sample description at developers.arcgis.com.
https://developers.arcgis.com/javascript/latest/popup-actions/index.html
-->
<title>Define custom Popup actions - 4.2</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</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/Map",
"esri/views/MapView",
"esri/geometry/Point",
"dojo/domReady!"
], function(
Map,
MapView,
Point
) {
// Create the Map
var map = new Map({
basemap: "topo"
});
// Create the MapView
var view = new MapView({
container: "viewDiv",
map: map,
center: [-117, 34],
zoom: 9
});
view.then(function() {
var peak = new Point({
latitude: 34.09916,
longitude: -116.82485
});
view.popup.open({
location: peak,
title: "San Gorgonio Mountain",
content: "Tallest peak in Southern California"
});
});
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>
&#13;