Nov 20, 2010 at 9:25 AM
Edited Nov 20, 2010 at 12:15 PM
I inserted my rectangle to geoMap1 with this code on buttonclicked, works Ok for me:
Coordinate loc = new Coordinate();
loc.X = 520501.4;
loc.Y = 122814.2;
System.Drawing.Point p1 = geoMap1.ProjToPixel(loc);
Graphics g = geoMap1.CreateGraphics();
Brush fill = new SolidBrush(Color.FromArgb(60, 192, 192, 192));
g.DrawRectangle(Pens.Black, p1.X - 15, p1.Y - 12, 30, 24);
g.FillRectangle(fill, p1.X - 15, p1.Y - 12, 30, 24);
But when located this code into public void ExposeOurTextBoxes(...) on frmMapWindow6.cs nothing happened?
What to do that graphics will apper on the map and how to catch the handle or something else to remove this graphics from map?
>>>> Just found the very good article "Draw On the Map" and solved first part of my problem. But how to erase or remove this rectangle from map?
Thanks for any help and best regards, Vojko
Nov 20, 2010 at 2:31 PM
It sounds like you found an article clarifying not to use geoMap1.CreateGraphics(). For those that don't know, you are just sketching on top of the latest refresh of the control with that, and you will lose content on a refresh. Either 1) handle
the map1.Paint event, 2) add drawing layers, 3) subclass MapFunction a, or 4) subclass the map and override OnPaint.
1) Adding a handler to the map1.Paint event means that whenever the control is invalidated, either by someone dragging something on top of your window or else by a resize, extent change, or symbology change, the map will be repainted. Most of the time,
repainting is done for smaller client regions than the whole screen, so it can be useful to pay attention to the "ClipRectangle" argument passed in. You can use the "Graphics" property on the PaintEventArgs in order to draw just about
anything in screen coordinates. You can use the Map.ProjToPixel in order to figure out where to draw stuff on the screen at that particular time. To stop drawing the content, simply disable your drawing code with a boolean value or something and
then call map1.Invalidate()
2) To draw more complex georeferenced polygons than a rectangle that won't appear in the legend you can create a new Polygon. Create a new Feature from the Polygon. Create a new FeatureSet and add your feature to its Features list. Create
a new MapPolygonLayer using the FeatureSet. Change the Symbolizer or Symbology to your liking. Then add the polygon to map1.MapFrame.DrawingLayers. To stop drawing the polygon in this case, remove the MapPolygonLayer from the DrawingLayers.
3) MapFunctions are usually reserved for situations where you want additional behavior as well as drawing. Create a class and inherit from MapFunction. Use the Overrides in order to handle what happens during various mouse events. Override
OnDraw to draw something related to your MapFunction. This will occur in a sequence in the map1.MapFunctions list, so you can control who draws first based on the order in the map1.MapFunctions list. There is an enumeration called YieldStyle that
determines when your MapFunction will be deactivated, so if someone starts to Pan and you don't want the Pan instructions to be sent to your MapFunction, you can add YieldStyles.LeftButton, so that your function will yield to panning and become disabled.
AlwaysOn is good for glyphs that hang out on the map at all times that someone can click. To prevent your function from drawing, remove your MapFunction from the MapFunctions list and then do map1.Invalidate.
4) Subclassing the map and handling OnPaint is exactly the same outcome as handlng the Paint event. Both get you a back buffer to draw on. You are not fighting with what is being drawn natively in either case, so you don't create any flicker
by doing this. (As opposed to if you used map1.CreateGraphics()). To stop drawing, track a boolean or something, like with the paint event handler, and then hit map1.Invalidate();
Ted, thanks for your directions. I will surely use these for my next problem. For now I solved the problem with "_showCircle" from "Draw on the Map". I need rectangle only to show for a moment and then remove it from the map.