How Can i render map on ajaxMapControl?

Topics: SharpMap Project, SharpMap v0.9 / v1.x, Web Controls
Sep 4, 2009 at 7:00 AM

/*code from web demo

private SharpMap.Styles.VectorStyle GetCountryStyle(SharpMap.Data.FeatureDataRow row)
{
SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
switch (row["NAME"].ToString().ToLower())
{
case "denmark": //If country name is Danmark, fill it with green
style.Fill = Brushes.Green;
return style;
case "united states": //If country name is USA, fill it with Blue and add a red outline
style.Fill = Brushes.Blue;
style.Outline = Pens.Red;
return style;
case "china": //If country name is China, fill it with red
style.Fill = Brushes.Red;
return style;
default:
break;
}
//If country name starts with S make it yellow
if (row["NAME"].ToString().StartsWith("S"))
{
style.Fill = Brushes.Yellow;
return style;
}
// If geometry is a (multi)polygon and the area of the polygon is less than 30, make it cyan
else if (row.Geometry.GetType() == typeof(SharpMap.Geometries.MultiPolygon) &&
(row.Geometry as SharpMap.Geometries.MultiPolygon).Area < 30 ||
row.Geometry.GetType() == typeof(SharpMap.Geometries.Polygon) &&
(row.Geometry as SharpMap.Geometries.Polygon).Area < 30 )
{
style.Fill = Brushes.Cyan;
return style;
}
else //None of the above -> Use the default style
return null;
}


SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetCountryStyle);
myVectorLayer.Theme = iTheme;
*/

The code can be work in bin.aspx,but when i try to use it into ajax project,it is not work. How can i render map on ajaxMapControl?Thank you for your help!

Coordinator
Sep 8, 2009 at 5:47 PM

My guess is that you haven't mapped the httphandler - see the httpHandlers element in the demo website's web.config file and ensure you have an equivalent in your project. hth jd

Sep 9, 2009 at 2:01 AM

The following is my codes:
    private SharpMap.Styles.VectorStyle GetCountryStyle(SharpMap.Data.FeatureDataRow row)
    {
        SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
        switch (row["FIRE_RANK"].ToString().ToLower())
        {
            case "1": 
                style.Fill = Brushes.Green;
                return style;

            case "2": 
                style.Fill = Brushes.Blue;
                return style;

            case "3": 
                style.Fill = Brushes.Yellow;
                return style;

            case "4":
                style.Fill = Brushes.Orange;
                return style;

            case "5":
                style.Fill = Brushes.Red;
                return style;

            default:
                style.Fill = Brushes.White;
                return style;
               
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //ajaxMap.Map = MapHelper.InitializeMap(new System.Drawing.Size(10, 10));
        ajaxMap.Map = MapHelper.InitializeMap(new System.Drawing.Size(10, 10));
        SharpMap.Rendering.Thematics.CustomTheme iTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetCountryStyle);
        SharpMap.Styles.VectorStyle defaultstyle = new SharpMap.Styles.VectorStyle();
        defaultstyle.Fill = Brushes.White;
        iTheme.DefaultStyle = defaultstyle;
        (ajaxMap.Map.Layers[0] as SharpMap.Layers.VectorLayer).Theme = iTheme;
       
        if (!Page.IsPostBack && !Page.IsCallback)
        {
            //Set up the map. We use the method in the App_Code folder for initializing the map
            ajaxMap.Map.Center = new SharpMap.Geometries.Point(116.0, 27.0);
            ajaxMap.FadeSpeed = 10;
            ajaxMap.ZoomSpeed = 10;
            ajaxMap.Map.Zoom = 5.2;
         }

        ajaxMap.ResponseFormat = "maphandler.ashx?MAP=SimpleWorld&Width=[WIDTH]&Height=[HEIGHT]&Zoom=[ZOOM]&X=[X]&Y=[Y]";

    }

sharpMap didn't render map in 5 color, but rendered by only 1 color that be defined in MapHelper.cs file. I changed  web.config file according your suggestion, but it is not work yet .How can i do ? Thank you for your help!

Coordinator
Sep 9, 2009 at 2:47 PM

Put a breakpoint in the GetCountryStyle method and see what values the row actually has.. hth jd 

Sep 10, 2009 at 2:53 AM

The values of style that in the GetContryStyle are right. I can see the values by breakpoint. But I don't know why I can't render my map.

ajaxMap.ResponseFormat = "maphandler.ashx?MAP=SimpleWorld&Width=[WIDTH]&Height=[HEIGHT]&Zoom=[ZOOM]&X=[X]&Y=[Y]";

I think the codes only display the map ,but not rendering the map.The values of style seems always in cache. How can i do ?

Coordinator
Sep 11, 2009 at 12:45 PM

SharpMap 0.9 doesn't cache maps so make sure the browser or any proxy hasn't cached the rendered map - you can add a random string to the map request url to ensure that the image is always unique hth jd