Extracting location clicked from InfoWindow Content Formatting function

1997
2
05-20-2016 10:28 AM
SteveCole
Frequent Contributor


I've run into a  situation and I'm not sure I can do what I want. I'm using formatting functions for my infoWindow content and, as part of that content, I'm including spatial coordinates (lat/long & US National Grid). If the feature clicked is a point, all is well. If the feature is a line or a polygon, things get harder because the geometry of the graphic isn't where the user actually clicked.

Is there any way, from within a formatting function, to go back up the chain of events to get the map click location? I tried setting a map.on("click", function(evt) {}) to store the map click position in a global variable but that's occuring AFTER I get to the content formatting function.

I'm not seeing a way to backtrack via the graphic sent to the function but I thought I'd ask.

Steve

0 Kudos
2 Replies
thejuskambi
Occasional Contributor III

I am not sure what you are trying to do. Are you trying to show the Lat Long? coordinates where the user clicked in the infoWindow. Is my understanding correct?

Could you share some code.

0 Kudos
SteveCole
Frequent Contributor

Yes, I think your understanding is correct. I am trying to add x,y coordinates into the content within an infoWindow for point, line, and polygon layers. The coordinates would be for the location that the user initially clicked. Some example code:

        var bridgeTemplate = new InfoTemplate();
        bridgeTemplate.setContent(formatBridgeContent);
        bridgeTemplate.setTitle("County Bridges");
        bridgeLayer.setInfoTemplate(bridgeTemplate);

        function formatBridgeContent(graphic) {
            attrib = graphic.attributes;
            latLongGeom = webMercatorUtils.webMercatorToGeographic(graphic.geometry);
            
            u = new USNG2();
            var theLatLong = [];
            theLatLong["lon"] = latLongGeom.x;
            theLatLong["lat"] = latLongGeom.y;
            var theUSNG = u.fromLonLat(theLatLong,5).toString();
            
            content = "<table><tr><td style=\"font-weight:bold;text-align:left\">Name:</td><td>" + attrib["GIS_FEATURES.GDBA.transportation_bridges.BRIDGE_NM"] + "</td></tr>";
            content = content + '<tr><td valign=\'top\' style=\"font-weight:bold;padding-left:3px;padding-right:3px;vertical-align:top\">Facility Carried:</td><td valign=\'top\' style=\"padding-left:3px;padding-right:3px;vertical-align:top\">' + attrib["GIS_FEATURES.GDBA.transportation_bridges.FAC_CARRI"] + '</td></tr>';
            content = content + '<tr><td valign=\'top\' style=\"font-weight:bold;padding-left:3px;padding-right:3px;vertical-align:top\">Location:</td><td valign=\'top\' style=\"padding-left:3px;padding-right:3px;vertical-align:top\">' + attrib["GIS_FEATURES.GDBA.transportation_bridges.LOCATION"] + '</td></tr>';            
            content = content + "<tr><td style=\"font-weight:bold;text-align:left\">Seismic Risk:</td><td>" + attrib["snoco_bridge_seismic_risk.seismcRisk"] + "</td></tr>";
            content = content + "<tr><td style=\"font-weight:bold;text-align:left\">Bridge Number:</td><td>" + attrib["GIS_FEATURES.GDBA.transportation_bridges.BRIDGE_ID"] + "</td></tr>";
            content = content + "<tr><td> </td><td> </td></tr>";            
            content = content + "<tr><td style=\"font-weight:bold;text-align:left\">Latitude/Longitude:</td><td>" + latLongGeom.y.toFixed(5) + ", " + latLongGeom.x.toFixed(5) + "</td></tr>";
            content = content + "<tr><td style=\"font-weight:bold;text-align:left\">US National Grid:</td><td>" + theUSNG + "</td></tr></table>";


            return content;
        }  

USNG2 is a javascript library I found which converts lat/long coordinates to U.S. National Grid Coordinates. Here's a screenshot of what this looks like:

infoWindow_w_coord_example.jpg

0 Kudos