ThinkGeo.com    |     Documentation    |     Premium Support

Pulling names and values from thinkgeo collection

Hi, i am trying to create a json object from a collection but keep running into errors when tryting to format it. is there a way to pull the ids and values from a collection? i am trying to get rid of the pop up and change it to a table so that the user can see it outside the map area. any help would be greatly appreciated.


public string ClickEvent(Map map, GeoCollection<object> args,ParcelInfo info)
       {
           // Argument reference:
           // 0: World longitude coordinate (x)
           // 1: World latitude coordinate (y)
           // 2: Current scale of the map
 
           string popupJson = string.Empty;
           int clickRadius;
           double currentScale;
           PointShape position = new PointShape(Convert.ToDouble(args[0]), Convert.ToDouble(args[1]));
 
           // Wilson Parcel Overlay
           LayerOverlay overlay = (LayerOverlay)map.CustomOverlays[“Wilson Parcels”];
           ShapeFileFeatureLayer inm = overlay.Layers[0] as ShapeFileFeatureLayer;
           inm.Open();
           InMemoryFeatureLayer highlightLayer = overlay.Layers[“HighlightLayerP”] as InMemoryFeatureLayer;
           highlightLayer.Open();
 
           // Wilson Subdivision  Overlay
           LayerOverlay overlayS = (LayerOverlay)map.CustomOverlays[“Wilson Subdivisions”];
           ShapeFileFeatureLayer inm2 = overlayS.Layers[0] as ShapeFileFeatureLayer;
           inm2.Open();
           InMemoryFeatureLayer highlightLayerSub = overlayS.Layers[“HighlightLayerSub”] as InMemoryFeatureLayer;
           highlightLayerSub.Open();
           //Wilson Townships
           LayerOverlay overlayT = (LayerOverlay)map.CustomOverlays[“Wilson TownShips”];
           ShapeFileFeatureLayer inm3 = overlayT.Layers[0] as ShapeFileFeatureLayer;
           inm3.Open();
           InMemoryFeatureLayer highlightLayerTown = overlayT.Layers[“HighLightLayerTown”] as InMemoryFeatureLayer;
           highlightLayerTown.Open();
           //Wilson Sections
           LayerOverlay overlaySec = (LayerOverlay)map.CustomOverlays[“Wilson Sections”];
           ShapeFileFeatureLayer inm4 = overlaySec.Layers[0] as ShapeFileFeatureLayer;
           inm4.Open();
           InMemoryFeatureLayer highlightLayerSec = overlaySec.Layers[“HighLightLayerSec”] as InMemoryFeatureLayer;
           highlightLayerSec.Open();
           if (overlay.IsVisible != false)
           {
               // Get a list of all the columns in the parcel layer (we’ll need this for the GetFeaturesNearestTo method)
               Collection<FeatureSourceColumn> parcelLayerColumns = inm.FeatureSource.GetColumns();
               List<string> parcelLayerColumnNames = new List<string>();
               foreach (FeatureSourceColumn col in parcelLayerColumns)
               {
                   parcelLayerColumnNames.Add(col.ColumnName);
               }
 
               // Adjust the click radius based on the scale of the map (via event args) for better UX – capped at 6400 meters (approx. 4 miles)
               if (Double.TryParse(args[2].ToString(), out currentScale) && Int32.TryParse(Math.Ceiling(currentScale / 100).ToString(), out clickRadius))
               {
                   if (clickRadius > 6400 || clickRadius <= 0)
                   {
                       clickRadius = 6400;
                   }
               }
               else
               {
                   clickRadius = 6400;
               }
 
               // Create a new CloudPopup and set some common properties
               CloudPopup infoPopup = new CloudPopup(“ParcelInfoPopup”);
               infoPopup.HasCloseButton = true;
 
               infoPopup.AutoPan = true;
               infoPopup.AutoSize = true;
 
               // Open the highlightLayer for editing and clear any existing highlight points
               highlightLayer.FeatureSource.Open();
               highlightLayer.FeatureSource.BeginTransaction();
               highlightLayer.InternalFeatures.Clear();
 
               // Use GetFeaturesNearestTo instead of GetFeaturesWithinDistanceOf so that the first feature returned is always closest to the click point
               Collection<Feature> closestFeature = inm.FeatureSource.GetFeaturesNearestTo(position, map.MapUnit, 1, parcelLayerColumnNames as IEnumerable<string>, clickRadius, DistanceUnit.Meter);
                
               inm.Close();
               if (closestFeature.Any())
               {
                   // Add a copy of this feature to the highlightLayer
                   highlightLayer.FeatureSource.AddFeature(closestFeature[0]);
                   info.ParcelID = closestFeature[0].ColumnValues[“PARCELID”].Trim();
                    //Create a popup with info about this feature
                   StringBuilder message = new StringBuilder();
                   message.AppendFormat("<li>Parcel Id: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“PARCELID”].Trim());

                   message.AppendFormat("<li>Owner: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“OWNER”].Trim());

                   //message.AppendFormat("<li>Owner Addr: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“OWNADDR”].Trim());

                   message.AppendFormat("<li>Quick Ref ID: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“QuickRefID”].Trim());

                   message.AppendFormat("<li>Acres: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“CalcAcres”].Trim());

                   message.AppendFormat("<li>Tax Unit : {0}<!–</code–>li>", closestFeature[0].ColumnValues[“TAXUNIT”].Trim());

                   message.AppendFormat("<input name=‘TaxData’ type=‘button’ id=‘btnCIC’ value=‘Tax Data’ onclick=‘test1()’/>");
                   message.AppendFormat("<input name=‘OrionData’ type=‘button’ id=‘btnOrion’ value=‘Orion Data’ onclick=‘test1()’/>");
                   message.AppendFormat("<input name=‘Photo’ type=‘button’ id=‘btnPhoto’ value=‘Photo’ onclick=‘test1()’/>");
                   message.AppendFormat("<input name=‘SVQ’ type=‘button’ id=‘btnSvS’ value=‘SVQ’ onclick=‘test1()’/>");
                   string messageInPopup = String.Format("<div class=\&#34;normalBlueTx\&#34;>{0}<!–</code–>div>", message.ToString());

 
                   infoPopup.ContentHtml = messageInPopup;
                   infoPopup.Position = closestFeature[0].GetShape().GetCenterPoint();
               }
               else
               {
                   // Create a default instructional popup
                   infoPopup.ContentHtml = “<div class=\&#34;normalBlueTx\&#34;>Please click on a Parcel to show its information.<!–</code–>div>”;

                   infoPopup.Position = position;
               }
 
               // Commit changes to the highlightLayer
               highlightLayer.FeatureSource.CommitTransaction();
               highlightLayer.FeatureSource.Close();
 
               // Clear the map’s Popups collection and add our new one
               map.Popups.Clear();
               map.Popups.Add(infoPopup);
 
               // Convert the popup to JSON and return it to the client
               popupJson = infoPopup.ToJson();
               var x = popupJson;
 
               return popupJson;
           }
 
           if (overlayS.IsVisible != false)
           {
 
               // Get a list of all the columns in the parcel layer (we’ll need this for the GetFeaturesNearestTo method)
               Collection<FeatureSourceColumn> SubdivisionLayerColumns = inm2.FeatureSource.GetColumns();
               List<string> SubdivisionLayerColumnNames = new List<string>();
               foreach (FeatureSourceColumn col in SubdivisionLayerColumns)
               {
                   SubdivisionLayerColumnNames.Add(col.ColumnName);
               }
 
               // Adjust the click radius based on the scale of the map (via event args) for better UX – capped at 6400 meters (approx. 4 miles)
               if (Double.TryParse(args[2].ToString(), out currentScale) && Int32.TryParse(Math.Ceiling(currentScale / 100).ToString(), out clickRadius))
               {
                   if (clickRadius > 6400 || clickRadius <= 0)
                   {
                       clickRadius = 6400;
                   }
               }
               else
               {
                   clickRadius = 6400;
               }
 
               // Create a new CloudPopup and set some common properties
               CloudPopup infoPopup = new CloudPopup(“SubdivisionInfoPopup”);
               infoPopup.HasCloseButton = true;
               infoPopup.AutoPan = true;
               infoPopup.AutoSize = true;
 
               // Open the highlightLayer for editing and clear any existing highlight points
               highlightLayerSub.FeatureSource.Open();
               highlightLayerSub.FeatureSource.BeginTransaction();
               highlightLayerSub.InternalFeatures.Clear();
 
               // Use GetFeaturesNearestTo instead of GetFeaturesWithinDistanceOf so that the first feature returned is always closest to the click point
               Collection<Feature> closestFeature = inm2.FeatureSource.GetFeaturesNearestTo(position, map.MapUnit, 1, SubdivisionLayerColumnNames as IEnumerable<string>, clickRadius, DistanceUnit.Meter);
               inm2.Close();
               if (closestFeature.Any())
               {
                   // Add a copy of this feature to the highlightLayer
                   highlightLayerSub.FeatureSource.AddFeature(closestFeature[0]);
 
                   // Create a popup with info about this feature
                   StringBuilder message = new StringBuilder();
                   message.AppendFormat("<li>Name:{0}<!–</code–>li>", closestFeature[0].ColumnValues[“NAME”].Trim());

                   message.AppendFormat("<li>Shape Length = {0}<!–</code–>li>", closestFeature[0].ColumnValues[“Shape_Leng”].Trim());

                   string messageInPopup = String.Format("<div class=\&#34;normalBlueTx\&#34;>{0}<!–</code–>div>", message.ToString());

 
                   infoPopup.ContentHtml = messageInPopup;
                   infoPopup.Position = closestFeature[0].GetShape().GetCenterPoint();
               }
               else
               {
                   // Create a default instructional popup
                   infoPopup.ContentHtml = “<div class=\&#34;normalBlueTx\&#34;>Please click on a Subdivision to show its information.<!–</code–>div>”;

                   infoPopup.Position = position;
               }
 
               // Commit changes to the highlightLayer
               highlightLayerSub.FeatureSource.CommitTransaction();
               highlightLayerSub.FeatureSource.Close();
 
               // Clear the map’s Popups collection and add our new one
               map.Popups.Clear();
               map.Popups.Add(infoPopup);
 
               // Convert the popup to JSON and return it to the client
               popupJson = infoPopup.ToJson();
               //return popupJson;
 
           }
           if (overlayT.IsVisible != false)
           {
 
               // Get a list of all the columns in the parcel layer (we’ll need this for the GetFeaturesNearestTo method)
               Collection<FeatureSourceColumn> TownShipLayerColumns = inm3.FeatureSource.GetColumns();
               List<string> TownShipLayerColumnNames = new List<string>();
               foreach (FeatureSourceColumn col in TownShipLayerColumns)
               {
                   TownShipLayerColumnNames.Add(col.ColumnName);
               }
 
               // Adjust the click radius based on the scale of the map (via event args) for better UX – capped at 6400 meters (approx. 4 miles)
               if (Double.TryParse(args[2].ToString(), out currentScale) && Int32.TryParse(Math.Ceiling(currentScale / 100).ToString(), out clickRadius))
               {
                   if (clickRadius > 6400 || clickRadius <= 0)
                   {
                       clickRadius = 6400;
                   }
               }
               else
               {
                   clickRadius = 6400;
               }
 
               // Create a new CloudPopup and set some common properties
               CloudPopup infoPopup = new CloudPopup(“TownShipInfoPopup”);
               infoPopup.HasCloseButton = true;
               infoPopup.HasCloseButton = true;
               infoPopup.AutoPan = true;
               infoPopup.AutoSize = true;
 
               // Open the highlightLayer for editing and clear any existing highlight points
               highlightLayerTown.FeatureSource.Open();
               highlightLayerTown.FeatureSource.BeginTransaction();
               highlightLayerTown.InternalFeatures.Clear();
 
               // Use GetFeaturesNearestTo instead of GetFeaturesWithinDistanceOf so that the first feature returned is always closest to the click point
               Collection<Feature> closestFeature = inm3.FeatureSource.GetFeaturesNearestTo(position, map.MapUnit, 1, TownShipLayerColumnNames as IEnumerable<string>, clickRadius, DistanceUnit.Meter);
               inm3.Close();
               if (closestFeature.Any())
               {
                   // Add a copy of this feature to the highlightLayer
                   highlightLayerTown.FeatureSource.AddFeature(closestFeature[0]);
 
                   // Create a popup with info about this feature
                   StringBuilder message = new StringBuilder();
                   message.AppendFormat("<li>Township: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“TOWNSHIP”].Trim());

                   message.AppendFormat("<li>Shape Length = {0}<!–</code–>li>", closestFeature[0].ColumnValues[“Shape_Leng”].Trim());

                   string messageInPopup = String.Format("<div class=\&#34;normalBlueTx\&#34;>{0}<!–</code–>div>", message.ToString());

 
                   infoPopup.ContentHtml = messageInPopup;
                   infoPopup.Position = closestFeature[0].GetShape().GetCenterPoint();
               }
               else
               {
                   // Create a default instructional popup
                   infoPopup.ContentHtml = “<div class=\&#34;normalBlueTx\&#34;>Please click on a TownShip to show its information.<!–</code–>div>”;

                   infoPopup.Position = position;
               }
 
               // Commit changes to the highlightLayer
               highlightLayerTown.FeatureSource.CommitTransaction();
               highlightLayerTown.FeatureSource.Close();
 
               // Clear the map’s Popups collection and add our new one
               map.Popups.Clear();
               map.Popups.Add(infoPopup);
 
               // Convert the popup to JSON and return it to the client
               popupJson = infoPopup.ToJson();
               return popupJson;
 
           }
           if (overlaySec.IsVisible != false)
           {
 
               // Get a list of all the columns in the parcel layer (we’ll need this for the GetFeaturesNearestTo method)
               Collection<FeatureSourceColumn> SectionsLayerColumns = inm4.FeatureSource.GetColumns();
               List<string> SectionsLayerColumnNames = new List<string>();
               foreach (FeatureSourceColumn col in SectionsLayerColumns)
               {
                   SectionsLayerColumnNames.Add(col.ColumnName);
               }
 
               // Adjust the click radius based on the scale of the map (via event args) for better UX – capped at 6400 meters (approx. 4 miles)
               if (Double.TryParse(args[2].ToString(), out currentScale) && Int32.TryParse(Math.Ceiling(currentScale / 100).ToString(), out clickRadius))
               {
                   if (clickRadius > 6400 || clickRadius <= 0)
                   {
                       clickRadius = 6400;
                   }
               }
               else
               {
                   clickRadius = 6400;
               }
 
               // Create a new CloudPopup and set some common properties
               CloudPopup infoPopup = new CloudPopup(“TownShipInfoPopup”);
               infoPopup.HasCloseButton = true;
               infoPopup.AutoPan = true;
               infoPopup.AutoSize = true;
 
               // Open the highlightLayer for editing and clear any existing highlight points
               highlightLayerSec.FeatureSource.Open();
               highlightLayerSec.FeatureSource.BeginTransaction();
               highlightLayerSec.InternalFeatures.Clear();
 
               // Use GetFeaturesNearestTo instead of GetFeaturesWithinDistanceOf so that the first feature returned is always closest to the click point
               Collection<Feature> closestFeature = inm4.FeatureSource.GetFeaturesNearestTo(position, map.MapUnit, 1, SectionsLayerColumnNames as IEnumerable<string>, clickRadius, DistanceUnit.Meter);
               inm4.Close();
               if (closestFeature.Any())
               {
                   // Add a copy of this feature to the highlightLayer
                   highlightLayerSec.FeatureSource.AddFeature(closestFeature[0]);
 
                   // Create a popup with info about this feature
                   StringBuilder message = new StringBuilder();
                   message.AppendFormat("<li>Township: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“TOWNSHIP”].Trim());

                   //message.AppendFormat("<li>Section: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“SECTION_”].Trim());

                   //message.AppendFormat("<li>Name: {0}<!–</code–>li>", closestFeature[0].ColumnValues[“POLTOWNSHIP”].Trim());

                   message.AppendFormat("<li>Shape Length = {0}<!–</code–>li>", closestFeature[0].ColumnValues[“Shape_Leng”].Trim());

                   string messageInPopup = String.Format("<div class=\&#34;normalBlueTx\&#34;>{0}<!–</code–>div>", message.ToString());

 
                   infoPopup.ContentHtml = messageInPopup;
                   infoPopup.Position = closestFeature[0].GetShape().GetCenterPoint();
               }
               else
               {
                   // Create a default instructional popup
                   infoPopup.ContentHtml = “<div class=\&#34;normalBlueTx\&#34;>Please click on a Section to show its information.<!–</code–>div>”;

                   infoPopup.Position = position;
               }
 
               // Commit changes to the highlightLayer
               highlightLayerSec.FeatureSource.CommitTransaction();
               highlightLayerSec.FeatureSource.Close();
 
               // Clear the map’s Popups collection and add our new one
               map.Popups.Clear();
               map.Popups.Add(infoPopup);
 
               // Convert the popup to JSON and return it to the client
               popupJson = infoPopup.ToJson();
               return popupJson;
 
           }
           return string.Empty;
           
       }


Hi Gordon,



If I misunderstand anything please let me know.



Because I don’t have your overlays, I just simplified you code and did a test, it looks we can return correct value by JSON and you should want to display that on page by some 3rd part library for example Jquery.js.



On server side, if you want to convert any data you want to JSON please try the “DataContractJsonSerializer”.



Any question please let me know.



Regards,



Don







Server side:

        [MapActionFilter]


       
public string ClickEvent(Map map, GeoCollection<object> args)


       
{


           
string
popupJson = string.Empty;


           
int clickRadius;


           
double
currentScale;


           
PointShape
position = new PointShape(Convert.ToDouble(args[0]), Convert.ToDouble(args[1]));


 


           
CloudPopup
infoPopup = new CloudPopup(“TownShipInfoPopup”);


           
infoPopup.HasCloseButton = true;


           
infoPopup.AutoPan = true;


           
infoPopup.AutoSize = true;


           
StringBuilder message = new StringBuilder();


           
message.AppendFormat(“

  • Township:
    {0}”, “1234567”);


               
    message.AppendFormat(“

  • Shape
    Length = {0}”, “7654321”);


               
    string
    messageInPopup = String.Format("<div
    class=\&#34;normalBlueTx\&#34;>{0}", message.ToString());


               
    infoPopup.ContentHtml = messageInPopup;


           
        infoPopup.Position =
    map.CurrentExtent.GetCenterPoint();


     


               
    popupJson = infoPopup.ToJson();


               
    return
    popupJson;


           
    }





    Client side:


    <script language=“javascript” type=“text/javascript”>


       
    function
    mapClick(e) {


           
    var params = { x:
    e.worldXY.lon, y: e.worldXY.lat };


     


           
    Map1.ajaxCallAction(’@ViewContext.RouteData.Values[“Controller”].ToString()’, ‘ClickEvent’,
    params, mapCallback);


       
    }


     


       
    function
    mapCallback(result) {


           
    var a =
    result.get_responseData();


           
    alert(a);


           
    Map1.redrawLayer(‘MarkerOverlay’);


       
    }




    </script>