Search code examples
c#winformsgoogle-earth-plugin

Boilerplate code for listening to MouseEvents in Google Earth COM API


I'm trying to get the LAT/LON position of the mouse in a Windows Forms app using the browsercontrol and IGEPlugin. Anyone got a clue?


Solution

  • This isn't too hard if you use the free Winforms Geplugin control librar y- just follow these simple steps

    You need to tell the web browser object that you want to listen for mousemove events

     geWebBrowser.AddEventListener(gePlugin.getGlobe(), "mousemove");
    

    Then you need setup some event handlers. The code below should be easy to read. You can determine the lat / long of the mouse cursor from the mouseEvent argument in the DoMouseMove method

    geWebBrowser.KmlEvent += GeWebBrowserKmlEvent;
    
    
    private void GeWebBrowserKmlEvent(object sender, GEEventArgs e)
            {
                // if it is a mouse event
                if (null != sender as IKmlMouseEvent)
                {
                    handleKmlMouseEvents((IKmlMouseEvent)sender, e.Data);
                }
                else
                {
                    MessageBox.Show(GEHelpers.GetTypeFromRcw(sender));
                }
            }
    
     private void handleKmlMouseEvents(IKmlMouseEvent mouseEvent, string action)
            {
                string currentTarget = mouseEvent.getCurrentTarget().getType();
    
                switch (action)
                {
                    case "mousemove":
                        {
                            DoMouseMove(mouseEvent);
                            break;
                        }
    
                    case "click":
                        {
                            DoClick(mouseEvent, currentTarget);
                            break;
                        }
                    case "mousedown":
                        {
                            DoMouseDown(mouseEvent, currentTarget);
                            break;
                        }
                    case "mouseup":
                        {
                            DoMouseUp(mouseEvent);
                            break;
                        }
                }
            }
    
     private void DoMouseMove(IKmlMouseEvent mouseEvent)
     {
    
     }