ThinkGeo.com    |     Documentation    |     Premium Support

Loading markers from controller

Hello,



I’m new to MapSuite and am working through some proof-of-concept testing with the trial MVC edition. I’m trying to load around 1500 markers into a Google map from a list of entities in my controller. For some reason the markers never show up. Here are the relevant bits of my code:



View


@using ThinkGeo.MapSuite.MvcEdition
@using ThinkGeo.MapSuite.Core
@using System.Configuration;
 
@{
    ViewBag.Title = “GIS Test”;
}
 
<script>
    function convertCoords(lon, lat) {
        var fromProjection = new OpenLayers.Projection(“EPSG:4326”); // Transform from WGS 1984
        var toProjection = new OpenLayers.Projection(“EPSG:900913”); // to Spherical Mercator Projection
        var position = new OpenLayers.LonLat(parseFloat(lon), parseFloat(lat)).transform(fromProjection, toProjection);
 
        return position;
    }
 
    $(document).ready(function () {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                MainMap.setCenter(convertCoords(position.coords.longitude, position.coords.latitude), 13);
            });
        }
 
        MainMap.ajaxCallAction(“gis/gis.aspx”, “GetLocations”, { StartDate: Date.now(), EndDate: Date.now() }, function (ajaxResponse) {
 
 
            MainMap.redrawLayer(“DynamicOverlay”);
        });
    });
</script>
 
<div>
    @{
        Html.ThinkGeo().Map(“MainMap”, new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 900)
        .MapBackground(new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))))
        .CurrentExtent(-13939426.6371, 6701997.4056, -7812401.86, 2626987.386962)
        .MapUnit(GeographyUnit.Meter) 
        .MapTools(mapTools =>
        {
            mapTools.OverlaySwitcherMapTool().Enabled(true);
            mapTools.MouseCoordinateMapTool().Enabled(true);
        })  
        .CustomOverlays(overlay => 
        {
            overlay.GoogleOverlay(“Google Map”).GoogleMapType(GoogleMapType.Normal).JavaScriptLibraryUri(new Uri(ConfigurationManager.AppSettings[“gisGoogleUri”]));
             
            overlay.InMemoryMarkerOverlay(“DynamicOverlay”)  
                .ZoomLevelSet(z =>  
                {
                    z.ZoomLevel01.DefaultMarkerStyle.WebImage = new ThinkGeo.MapSuite.MvcEdition.WebImage(21, 25, -10.5f, -25f);
                    z.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
                }); 
        })
        .Render();
    }
</div>

Controller


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Runtime.Serialization.Json;
using GIS.Models;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.MvcEdition;
using System.IO;
 
namespace GIS.Controllers
{
    public class GISController : Controller
    {
        //
        // GET: /GIS/
 
        public ActionResult Index()
        {
            return View();
        }
 
        [MapActionFilter]
        public void GetLocations(Map map, GeoCollection<object> args)
        {
            var locationEntities = new GIS.Models.GISdb();
 
            if (map != null)
            {
                IEnumerable<Location> Locations = locationEntities.GetLocations(46, DateTime.Now, DateTime.Now);
                 
                InMemoryMarkerOverlay markerOverlay = map.CustomOverlays[“DynamicOverlay”as InMemoryMarkerOverlay;
 
                foreach (Location location in Locations)
                {
                    if (!markerOverlay.FeatureSource.InternalFeatures.Contains(location.LocationID.ToString()))
                    {
                        markerOverlay.FeatureSource.InternalFeatures.Add(location.LocationID.ToString(), new Feature(location.Latitude, location.Longitude));
                    }
                }
            }
        }
    }
}

I’ve confirmed that the expected Location objects are being returned by my DAL.



When I debug the page using firebug, I see that the result of the GET call to markers_GeoResource.axd… returns:

[{“id”:“DynamicOverlay”, “markers”:[]}]


I’m assuming this should be populated with the markers, no?



Maybe I’m missing something simple, but I put this together based on the marker display sample.





Thanks!


Hi Kenneth, 
  
 Your code don’t have problem exception the locations shouldn’t in current extent. 
  
 As below is my test code based on your code. Please try it and see whether it works for you. 
  
 View 
  
@using ThinkGeo.MapSuite.MvcEdition
@using ThinkGeo.MapSuite.Core
@using System.Configuration;
<script type=“text/javascript”>
    function btnPlot_Click() {        
        MainMap.ajaxCallAction(’@ViewContext.RouteData.Values[“Controller”].ToString()’, ‘GetLocations’, { StartDate: Date.now(), EndDate: Date.now() }, function (ajaxResponse) {
            MainMap.redrawLayer(“DynamicOverlay”);
        })
    }
</script>
<div class=“four columns” id=“navigation”>
    @{Html.RenderPartial(“SourceCode”);}
    <input type=“button” id=“btnPlot” name=“btnPlot” value=“Plot” onclick=“btnPlot_Click();” />
</div>
<div class=“twelve columns” id=“main”>
    <div>
        @{
            Html.ThinkGeo().Map(“MainMap”, new System.Web.UI.WebControls.Unit(100, System.Web.UI.WebControls.UnitType.Percentage), 900)
            .MapBackground(new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))))
            .CurrentExtent(-13939426.6371, 6701997.4056, -7812401.86, 2626987.386962)
            .MapUnit(GeographyUnit.Meter)
            .MapTools(mapTools =>
            {
                mapTools.OverlaySwitcherMapTool().Enabled(true);
                mapTools.MouseCoordinateMapTool().Enabled(true);
            })
            .CustomOverlays(overlay =>
            {
                overlay.InMemoryMarkerOverlay(“DynamicOverlay”)
                    .ZoomLevelSet(z =>
                    {
                        z.ZoomLevel01.DefaultMarkerStyle.WebImage = new ThinkGeo.MapSuite.MvcEdition.WebImage(21, 25, -10.5f, -25f);
                        z.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
                    });
            })
            .Render();
        }
    </div>
</div>

 
  
 Controller 
  
using System.Collections.ObjectModel;
using System.Web.Mvc;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.MvcEdition;

namespace CSharp_HowDoISamples
{
    public partial class BackgroundMapsController : Controller
    {
        public ActionResult DisplayASimpleMap()
        {
            return View();
        }

        [MapActionFilter]
        public void GetLocations(Map map, GeoCollection<object> args)
        {
            //var locationEntities = new GIS.Models.GISdb();
            if (map != null)
            {
                //IEnumerable Locations = locationEntities.GetLocations(46, DateTime.Now, DateTime.Now);
                Collection<PointShape> ps = new Collection<PointShape>();
                ps.Add(new PointShape(-10875914.24855, 3664492.396281));
                ps.Add(new PointShape(-11875914.24855, 4664492.396281));
                ps.Add(new PointShape(-12875914.24855, 5664492.396281));
                ps.Add(new PointShape(-13875914.24855, 6664492.396281));

                InMemoryMarkerOverlay markerOverlay = map.CustomOverlays[“DynamicOverlay”] as InMemoryMarkerOverlay;

                foreach (PointShape location in ps)
                {
                    if (!markerOverlay.FeatureSource.InternalFeatures.Contains(location.Id.ToString()))
                    {
                        markerOverlay.FeatureSource.InternalFeatures.Add(location.Id.ToString(), new Feature(location.X, location.Y));
                    }
                }
            }
        }
    }
}
 
  
 Regards, 
  
 Don