Search code examples
arcgisarcgis-js-api

How to open popup at (lng,lat) position in arcgis 4.0


Let's consider this code from the reference (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",
});

This works. But how to open a popup at the point, specified by predefined lng,lat coords?

First try:

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

This does not work. The reason is that created point currently disconnected from map view. Is there a way to receive screen coords (x,y) of the current view by specified (lng,lat)? In google maps api there're methods like latLngToDivPixel, latLngToDivPoint, so what argis offers for this task?


Solution

  • Looks like you're having a SpatialReference issue. Since you're creating the point via lat/lng, it's not in WebMercator, so when you add it to the map it's going to the wrong place. Here's a fixed code for you:

    // 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"
    });
    

    Here is the above code in an example.