Why Address is showing as undefined?

2258
3
Jump to solution
05-10-2017 01:37 PM
ManjariGoyal
New Contributor III

I am trying to click on map and get lat, long and address in one popup window, but its not getting the address. Attached is the screenshot. Any help is appreciated.

var locator = new Locator("https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");
     var infoTemplate = new InfoTemplate("Location", "Address: ${Address}<br/>" +
     "City: ${City}<br/>" +
     "State: ${Region}<br/>");
     var symbol = new SimpleMarkerSymbol().setStyle(
      SimpleMarkerSymbol.STYLE_CIRCLE).setColor(
      new Color([0,0,255,0.5])
      );
     locator.on("location-to-address-complete", function(evt) {
      map.graphics.clear();
      console.log(evt);
       if (evt.address.address) {
      var address = evt.address.address;
      var location = webMercatorUtils.geographicToWebMercator(evt.address.location);
      var graphic = new Graphic(location, symbol, address, infoTemplate);
      map.graphics.add(graphic);
     var screenPnt = map.toScreen(location);
      map.infoWindow.resize(300,100);
       } 
     });
     map.on("click", function(evt) {
     map.infoWindow.setTitle("Coordinates/Address");
     map.infoWindow.setContent("lat/lon : " + evt.mapPoint.y.toFixed(3) + ", " + evt.mapPoint.x.toFixed(3) + "<br>Address: " + evt.mapPoint.address);
     map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
     });

0 Kudos
1 Solution

Accepted Solutions
ManjariGoyal
New Contributor III

I have found the solution below is the code in which you can see the address and XY in the same popup window.

// Set up a locator task using the world geocoding service
var locator = new Locator("https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

map.on("click", function(evt) {
locator.locationToAddress(evt.mapPoint, 100);
});

locator.on('location-to-address-complete', function(evt) {
console.log(evt);
var location = webMercatorUtils.geographicToWebMercator(evt.address.location)
var screenPoint = map.toScreen(location);
console.log(screenPoint);

      // dig out the best attribute from matched address to display in popup
map.infoWindow.setTitle("Coordinates/Address");
map.infoWindow.setContent("Latitude: " + evt.address.location.x.toFixed(3) + "<br>Longitude: " + evt.address.location.y.toFixed(3) + "<br> Address: " + evt.address.address.LongLabel);
map.infoWindow.show(screenPoint);

      // add an if/else and use the map click xy that you saved from the other event listener if no address was found
})

View solution in original post

0 Kudos
3 Replies
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hi Manjari Goyal,

I would suggest to open your browser developer tool to see what type of network traffic returns when you click on the map.

Ideally, you should be able to test with this reversegeocoding sample using JS 3.20:

Find Address 

Once you open the chrome developer tool and you will see the network traffic when you click on the map. The query request will send to https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode and if the address is not existing in this geocode service you will probably see  the error message like this:

The reason why some address is not return is due to the fact of the geocode coverage, for more information please check this online documentation:

Geocode coverage—ArcGIS REST API: World Geocoding Service | ArcGIS for Developers 

If you need specific address for your interest that not exist in our "World Geocoding Service", the best option is creating your own geocode service. More information please check: 

Geocode services—Documentation | ArcGIS Enterprise 

Hope this can help.

0 Kudos
ManjariGoyal
New Contributor III

Hi Yue Wu,

I have already looked in to this and the problem is I write two independent script one for geocode the address and other for reverse geocode it works fine but when I tried to create one script where I can get both the address and lat long and it doesn't work. below is the screenshot which I am trying to create in javascript 3.12

0 Kudos
ManjariGoyal
New Contributor III

I have found the solution below is the code in which you can see the address and XY in the same popup window.

// Set up a locator task using the world geocoding service
var locator = new Locator("https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

map.on("click", function(evt) {
locator.locationToAddress(evt.mapPoint, 100);
});

locator.on('location-to-address-complete', function(evt) {
console.log(evt);
var location = webMercatorUtils.geographicToWebMercator(evt.address.location)
var screenPoint = map.toScreen(location);
console.log(screenPoint);

      // dig out the best attribute from matched address to display in popup
map.infoWindow.setTitle("Coordinates/Address");
map.infoWindow.setContent("Latitude: " + evt.address.location.x.toFixed(3) + "<br>Longitude: " + evt.address.location.y.toFixed(3) + "<br> Address: " + evt.address.address.LongLabel);
map.infoWindow.show(screenPoint);

      // add an if/else and use the map click xy that you saved from the other event listener if no address was found
})

0 Kudos