Как открыть всплывающее окно в позиции (lng, lat) в arcgis 4.0

Рассмотрим этот код из справочника (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?

Первая попытка:

var point = new Point({latitude:lat,longitude:lng});
view.popup.open({
  location: point,
  title: "Some title"
});

Это не работает. Причина в том, что созданная точка в настоящее время отключена от просмотра карты. Есть ли способ получить координаты экрана (x, y) текущего представления по указанному (lng, lat)? В API карт Google есть такие методы, как latLngToDivPixel, latLngToDivPoint, так что же argis предлагает для этой задачи?


person Kasheftin    schedule 12.08.2016    source источник


Ответы (2)


Похоже, у вас проблема со 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"
});

Вот приведенный выше код в примере.

person GavinR    schedule 13.08.2016
comment
Альтернативой использованию webMercatorUtils было бы использование свойств широты и долготы в Point - см. developers.arcgis.com/javascript/latest/api-reference/. - person Bjorn Svensson; 31.08.2016

Приведенный выше ответ / проблема верен для версий 4.0 и 4.1.

Однако в недавно выпущенной версии 4.2 это было упрощено. Popup.location теперь автоматически конвертирует точки WGS84 (широта, долгота) в веб-меркатор, когда карта находится в проекции веб-меркатора.

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

person Bjorn Svensson    schedule 27.12.2016