【问题标题】:How to open popup at (lng,lat) position in arcgis 4.0如何在 arcgis 4.0 中的 (lng,lat) 位置打开弹出窗口
【发布时间】:2016-08-12 13:36:43
【问题描述】:

让我们考虑一下参考 (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为这个任务提供了什么?

【问题讨论】:

    标签: arcgis arcgis-js-api


    【解决方案1】:

    您似乎遇到了 SpatialReference 问题。由于您是通过 lat/lng 创建点,它不在 WebMercator 中,所以当您将它添加到地图时,它会出现在错误的位置。这是给你的固定代码:

    // this works, but needs to be in webmercator:
    // var point = new Point({x:-9035831.315416021, y:3095345.196351918});
    // as an alternative you can translate to webmercator on the fly:
    var point = webMercatorUtils.geographicToWebMercator(new Point({latitude:28.526622,longitude:-81.914063}));
    view.popup.open({
        location: point,
      title: "Some title"
    });
    

    这是an example中的上述代码。

    【讨论】:

    【解决方案2】:

    上面的答案/问题对于 4.0 和 4.1 版是正确的。

    但是,对于新发布的 4.2 版,这已被简化。现在,当地图处于 web 墨卡托投影中时,Popup.location 会自动将 WGS84(纬度、经度)点转换为 web 墨卡托。

    <!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>

    https://jsbin.com/heqotom/edit?html,output

    【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2012-12-10
    • 2019-02-18
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多