Select to view content in your preferred language

show coordinates won't work in 4.x

3793
2
04-20-2018 04:23 AM
KutayDeril
Emerging Contributor

I would like to show coordinates on map. I used these codes but I couldn't work. 

var map = new Map({
basemap: "streets",
layers: [tempGraphicsLayer]
});

var view = new MapView({
container: "viewDiv",
map: map,
center: [32.909550, 40.751906],
zoom: 9

});

function showCoordinates(event) {
//the map is in web mercator but display coordinates in geographic (lat, long)
var mp = webMercatorUtils.webMercatorToGeographic(event.mapPoint);
//display mouse coordinates
dom.byId("info").innerHTML = mp.x.toFixed(3) + ", " + mp.y.toFixed(3);
}

view.on("load", function () {
//after map loads, connect to listen to mouse move & drag events
alert("test");
view.on("mouse-move", showCoordinates);
view.on("mouse-drag", showCoordinates);
});

 <span id="info" style="position:absolute; left:100px; bottom: 100px; color:#0079c1; z-index:50;"></span>

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Kutay,

   There have been changes in the 4.7 API. Here is a sample re-worked for the changes:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Intro to MapView - Create a 2D map - 4.7</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.7/esri/css/main.css">
  <script src="https://js.arcgis.com/4.7/"></script>

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/geometry/support/webMercatorUtils",
      "dojo/dom",
      "dojo/domReady!"
    ], function(Map, MapView, webMercatorUtils, dom) {

      var map = new Map({
        basemap: "streets"
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65] // longitude, latitude
      });

      function showCoordinates(evt) {
        var point = view.toMap({x: evt.x, y: evt.y});
        //the map is in web mercator but display coordinates in geographic (lat, long)
        var mp = webMercatorUtils.webMercatorToGeographic(point);
        //display mouse coordinates
        dom.byId("info").innerHTML = mp.x.toFixed(3) + ", " + mp.y.toFixed(3);
      }

      view.when(function(){
        //after map loads, connect to listen to mouse move & drag events
        view.on("pointer-move", showCoordinates);
      });

    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
  <span id="info" style="position:absolute; left:100px; bottom: 100px; color:#0079c1; z-index:50;"></span>
</body>
</html>
UndralBatsukh
Esri Regular Contributor

Hi there, 

If you are using 4.7, you can take advantage of a new CoordinateConversion widget.