Draw polygon on Page Load

Sep 17, 2008 at 10:15 PM
I am able to draw the polygons on button click. Why is the same method not working when i call it in page load event? Is there something special that we need to set it up?
Please let us know.

Thanks
Chinkul

Developer
Sep 18, 2008 at 6:23 AM
Edited Sep 18, 2008 at 6:44 AM
The problem is that when the Page Load, UserControl Load or Map Load events fire off, the map is actually still in motion getting to it's default zoom and position.... therefore the pushpins dont seem to be added...

so, I was thinking that I need to know when the MotionFinished event was fired on the MultiScaleImage object that is displaying the map images....

so in the "DeepEarth" project I went to the "Controls" folder and opened the "Map.cs" class... and added this code...

   32         public MultiScaleImage msi;

  

   35         public event EventHandler OnMsiCreated;


Line 32 I just change the MultiScaleImage from "Internal" to "Public" so I could access this from my Silverlight Project

Then scroll down in the Map.cs class untill you find the "public override void OnApplyTemplate()"
this is where the MultiScaleImage msi object gets created, so I added to this method lines 519 - 522 shown below, so this fires an event that can tell you in your Silverlight application when the MultiScaleImage msi object is created

  506         public override void OnApplyTemplate()

  507         {

  508             base.OnApplyTemplate();

  509 

  510             msi = (MultiScaleImage)GetTemplateChild(PART_MultiScaleImage);

  511             licenseContainer = (Panel)GetTemplateChild(PART_LicenseContainer);

  512             adornerLayer = (Canvas)GetTemplateChild(PART_AdornerLayer);

  513             shapesCanvas = (Canvas)GetTemplateChild(PART_ShapesCanvas);

  514 

  515             if (msi == null)

  516             {

  517                 throw new InvalidOperationException();

  518             }

  519             else

  520             {

  521                OnMsiCreated(null, new EventArgs());

  522             }



then in the "DeepEarthPrototype" Project

in the "Page.cs" file I added these lines below,
in the Page() method I just added line 28 - "map.OnMsiCreated += new System.EventHandler(map_OnMsiCreated);"

"void map_OnMsiCreated" is fired when the msi object gets created, so now you are able to add a event handler for the MotionFinished event on the msi object, once the map motion stops you are able to add the pushpins or what ever you like to the map and they will show up.

"void msi_MotionFinished" is the event handler for the MotionFinished event, so here is where you add your pushpins etc to the map control... as the code in this example is actually adding a polygon

then the last line in this eventhandler method "map.msi.MotionFinished -= msi_MotionFinished;" is to remove the eventhandler for MotionFinshed event so you will not get a pushpin added every time the map control is zoomed...

   17         public Page()

   18         {

   19             InitializeComponent();

   20             map.BaseLayers.Add(new TileLayer(MapMode.Aerial));

   21             map.BaseLayers.Add(new TileLayer(MapMode.Hybrid));

   22             map.BaseLayers.Add(new TileLayer(MapMode.Road));

   23             map.BaseLayers.GoTo(0);

   24             map.EventBehavior.List.Insert(0, new PrototypeBehavior());

   25             map.EventBehavior.List.Insert(1, new MapModeBehavior());

   26             dashboard.MapControl = map;

   27 

   28             map.OnMsiCreated += new System.EventHandler(map_OnMsiCreated);

   29         }

   30 

   31         void map_OnMsiCreated(object sender, System.EventArgs e)

   32         {

   33             map.msi.MotionFinished +=new RoutedEventHandler(msi_MotionFinished);

   34         }

   35 

   36         void msi_MotionFinished(object sender, RoutedEventArgs e)

   37         {

   38             //Sample Polygon added to the map

   39             if (shapeLayer == null)

   40             {

   41                 shapeLayer = new ShapeLayer { ID = "POLYGONLAYER" };

   42                 map.Layers.Add(shapeLayer);

   43             }

   44             List<Point> points = new List<Point> { new Point(0, 0), new Point(20, 0), new Point(20, 20), new Point(0, 20) };

   45             Polygon polygon = new Polygon { Points = points };

   46             shapeLayer.List.Add(polygon);

   47 

   48             map.msi.MotionFinished -= msi_MotionFinished;

   49         }


OK, hope you can follow this example ;)
Developer
Sep 19, 2008 at 4:22 AM
The above sample seems to kill the view in Blend, so I updated to the code below which seems to fix this problem

in the "DeepEarth" project I went to the "Controls" folder and opened the "Map.cs" class...

  519             else

  520             {

  521                 if (System.Windows.Browser.HtmlPage.IsEnabled)

  522                 {

  523                     OnMsiCreated(null, new EventArgs());

  524                 }

  525             }

<!--EndFragment-->
Sep 19, 2008 at 3:05 PM
dotnetnoobie,

wanted to say thanks for all your info; you've bailed me out a few times.  DeepEarth has a lot going for it - you're helping to improve it.
Sep 21, 2008 at 3:01 AM
Thanks Dotnetnoobie

This works like charm.

Thanks