ThinkGeo.com    |     Documentation    |     Premium Support

Markers and context menu

Hello all,

I’m having an issue with the map control. I have a context menu that has a few options to work with the map. The problem is with the markers. 





Here’s what happens.


        
  1. Right click the map

  2.     
  3. The context menu shows, the markers are visible.

  4.     
  5. if you don’t select any of the options in the context menu and click somewhere else on the map to make the context menu disappear, then the markers will simply disappear.

  6.     
  7. Zooming or moving around the map will make the markers show again.




I even tried this on DesktopEdition_HOWDOI_CS solution, AddSimpleMarkers.cs. Add a ContextMenuStrip to the control, assign it to the map, and you’ll see. On this particular sample, you will have to wait until the PIN is added (takes about 2 seconds in my computer, not sure why it’s so slow), and then try to close the menu by click somewhere else (NOT clicking on one of the context menu options).



Can you please help me get rid of this bug?



Thanks!



Juan.

Hi Juan, 
  
 I think it’s should be a bug, I have reproduced that. 
  
 But it’s hard to find a workaround or fix, any update I will let you know. 
  
 Regards, 
  
 Don

Hi Juan, 
  
 This issue is very hard to fix, we will keep trying to solve it. 
  
 As below is a workaround for this issue. 
  
 Regards, 
  
 Don 
  
  using System;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.DesktopEdition;

namespace CSharpWinformsSamples
{
    public class ExtentInteractiveOverlayForContext : ExtentInteractiveOverlay
    {
        private bool contextMenuJustClosed = false;

        public bool ContextMenuJustClosed
        {
            get { return contextMenuJustClosed; }
            set { contextMenuJustClosed = value; }
        }

        protected override InteractiveResult MouseDownCore(InteractionArguments interactionArguments)
        {
            if (!contextMenuJustClosed)
            {
                return base.MouseDownCore(interactionArguments);
            }
            else
            {
                contextMenuJustClosed = false;
                return new InteractiveResult();
            }
        }
    }

    public partial class AddSimpleMarkers : UserControl
    {
        public AddSimpleMarkers()
        {
            InitializeComponent();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            ContextMenuStrip context = new System.Windows.Forms.ContextMenuStrip();
            context.Items.Add(" test1 ");
            context.Closed += context_Closed;

            winformsMap1.ContextMenuStrip = context;

            winformsMap1.ExtentOverlay = new ExtentInteractiveOverlayForContext();
            winformsMap1.MapClick += new EventHandler<MapClickWinformsMapEventArgs>(winformsMap1_MapClick);


            winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
            winformsMap1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9);

            WorldMapKitWmsDesktopOverlay worldMapKitOverlay = new WorldMapKitWmsDesktopOverlay();
            //winformsMap1.Overlays.Add(worldMapKitOverlay);

            SimpleMarkerOverlay markerOverlay = new SimpleMarkerOverlay();
            markerOverlay.MapControl = winformsMap1;
            winformsMap1.Overlays.Add("MarkerOverlay", markerOverlay);

            winformsMap1.Refresh();
        }

        void context_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            (winformsMap1.ExtentOverlay as ExtentInteractiveOverlayForContext).ContextMenuJustClosed = true;
        }


        private void winformsMap1_MapClick(object sender, MapClickWinformsMapEventArgs e)
        {
            if (e.MouseButton != MapMouseButton.Left)
            {                
                return;
            }


            SimpleMarkerOverlay markerOverlay = (SimpleMarkerOverlay)winformsMap1.Overlays["MarkerOverlay"];
            Marker marker = new Marker(e.WorldLocation);
            marker.Image = Properties.Resources.AQUA;
            marker.Width = 20;
            marker.Height = 34;
            marker.YOffset = -17;

            markerOverlay.Markers.Add(marker);

            winformsMap1.Refresh();
        }

        #region Component Designer generated code

        private System.ComponentModel.IContainer components = null;
        private GroupBox gbxDescrition;
        private WinformsMap winformsMap1;
        private Label label1;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.gbxDescrition = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.winformsMap1 = new ThinkGeo.MapSuite.DesktopEdition.WinformsMap();
            this.gbxDescrition.SuspendLayout();
            this.SuspendLayout();
            // 
            // gbxDescrition
            // 
            this.gbxDescrition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.gbxDescrition.Controls.Add(this.label1);
            this.gbxDescrition.Location = new System.Drawing.Point(480, -1);
            this.gbxDescrition.Name = "gbxDescrition";
            this.gbxDescrition.Size = new System.Drawing.Size(257, 55);
            this.gbxDescrition.TabIndex = 4;
            this.gbxDescrition.TabStop = false;
            this.gbxDescrition.Text = "Description";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(251, 26);
            this.label1.TabIndex = 0;
            this.label1.Text = "This sample shows how to add markers by click\r\n event on the map.";
            // 
            // winformsMap1
            // 
            this.winformsMap1.BackColor = System.Drawing.Color.White;
            this.winformsMap1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.winformsMap1.Location = new System.Drawing.Point(0, 0);
            this.winformsMap1.Name = "winformsMap1";
            this.winformsMap1.Size = new System.Drawing.Size(740, 528);
            this.winformsMap1.TabIndex = 5;
            this.winformsMap1.Text = "winformsMap1";
            // 
            // AddMyOwnCustomDataToAFeatureLayer
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.gbxDescrition);
            this.Controls.Add(this.winformsMap1);
            this.Name = "AddMyOwnCustomDataToAFeatureLayer";
            this.Size = new System.Drawing.Size(740, 528);
            this.Load += new System.EventHandler(this.Form_Load);
            this.gbxDescrition.ResumeLayout(false);
            this.gbxDescrition.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion
    }
}


Hi Juan,



Just want you know there is another option by using InMemoryMarkerOverlay instead of SimpleMarkerOverlay to avoid the issue without additional codes.




private void Form_Load(object sender, EventArgs e)
        {
            winformsMap1.MapClick += new EventHandler<MapClickWinformsMapEventArgs>(winformsMap1_MapClick);
            winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
            winformsMap1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9);
 
            ContextMenuStrip context = new System.Windows.Forms.ContextMenuStrip();
            context.Items.Add(" test1 ");
            winformsMap1.ContextMenuStrip = context;
            WorldMapKitWmsDesktopOverlay worldMapKitOverlay = new WorldMapKitWmsDesktopOverlay();
            winformsMap1.Overlays.Add(worldMapKitOverlay);
 
            InMemoryMarkerOverlay markerOverlay = new InMemoryMarkerOverlay();
            markerOverlay.MapControl = winformsMap1;
            markerOverlay.Columns.Add(new FeatureSourceColumn("Name"));
            markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.Image = Properties.Resources.AQUA;
            markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.Width = 20;
            markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.Height = 34;
            markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.YOffset = -17;
            markerOverlay.ZoomLevelSet.ZoomLevel01.DefaultPointMarkerStyle.ToolTipText = "This is [#Name#].";
            markerOverlay.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            winformsMap1.Overlays.Add("MarkerOverlay", markerOverlay);
 
            Feature newFeature = new Feature(-95.2806, 38.9554);
            newFeature.ColumnValues.Add("Name", "Lawrence");
 
            markerOverlay.FeatureSource.BeginTransaction();
            markerOverlay.FeatureSource.AddFeature(newFeature);
            markerOverlay.FeatureSource.CommitTransaction();
 
            winformsMap1.Refresh();
        }
 
        private void winformsMap1_MapClick(object sender, MapClickWinformsMapEventArgs e)
        {
            SimpleMarkerOverlay markerOverlay = (SimpleMarkerOverlay)winformsMap1.Overlays["MarkerOverlay"];
 
            markerOverlay.FeatureSource.BeginTransaction();
            markerOverlay.FeatureSource.AddFeature(new Feature(e.WorldLocation));
            markerOverlay.FeatureSource.CommitTransaction();
 
            winformsMap1.Refresh();
        }

Thanks,



Troy

Hi Troy and Don, thanks for helping, as you always do. 
  
 Troy, the problem with your solution is that my markers can have multiple images, so I really need to set that individually. I guess that will leave me with Don’s suggestion, right? 
  
 Didn’t have time to work on that just yet since I’m REALLY close to a delivery date (this Thursday) and there are still some missing features in the app. But I’ll work on this bug in about 2 weeks and will let you know how it goes. 
  
 BTW: our customer seems to be very happy with the new application and it’s mapping engine. So GO THINKGEO! ;) 
  
 Thanks again, 
  
 Juan.

Hi Juan,



Great to hear your customer loves our map and thanks for your praises, it encourages us a lot to make it better.  



The InMemoryMarkerOverlay supports multiple images and you just need to add more features into it. Then each feature represents a marker image.

The suggestion from me is using InMemoryMarkerOverlay and it has a big difference with SimpleMarkerOverlay, the markers added in SimpleMarkerOverlay are inherited from system.control and will results to the issue you mentioned somehow, I am hoping we can solve this bug in SimpleMarkerOvelay in the next days. However, using the InMemoryMarkerOverlay will avoid this issue completely as it don’t use the control marker.



Anyway, please feel free to let us know in the coming weeks if you have any questions. 



Thanks,



Troy

Hey guys, just FYI. I tried Don’s code, and it worked like a charm, with no much effort, just creating that class and adding the code to the Close event of the context menu.  
  
 Thanks a lot!

You are welcome, Juan.

This is great to hear that!  If any other questions, don’t hesitate to let us know.



Thanks,



Troy

Dear Juan,

Greetings of the Day!!!

Can you please share me this above application in ZIP format, since I am also even working on context menu for my project.

Thank you,

Ashok

Hi Ashok,

It looks that’s the reply one year ago, I am not sure whether Juan can find the project.

I think you can view my earlier reply which contains the code, it looks Juan’s solution is based on the code.

Regards,

Don