GMap custom marker and drawing of regular graphics

Source: Internet
Author: User
Preface

Previously written gmap Gmapoverlay class of simple use, mainly introduced markers,routes and polygons, about route and polygon no longer do too much to repeat, here, how to customize the marker new Project

New WinForm Project, add Gmap Reference (do not ask yourself Baidu), add the initialization code to the Gmap object in the Form1 constructor:

     This.gMapControl1.CacheLocation = System.Windows.Forms.Application.StartupPath;
            This.gMapControl1.MapProvider = Gmapproviders.binghybridmap;
            This.gMapControl1.Manager.Mode = Accessmode.serverandcache;
            This.gMapControl1.MinZoom = 1;//minimum ratio
            this.gMapControl1.MaxZoom = 23;//maximum proportion
            this.gMapControl1.Zoom = 15; Current scale
            This.gMapControl1.ShowCenter = false;//does not show center cross
            This.gMapControl1.DragButton = System.Windows.Forms.MouseButtons.Left;  Left-Drag map
            this.gMapControl1.Position = new POINTLATLNG (113);
            THIS.GMAPCONTROL1.OVERLAYS.ADD (overlay);

            This.gMapControl1.MouseClick + = Gmapcontrol1_mouseclick;

Here the author new project name is: Rulesofthegraphics custom Marker

In my previous blog I wrote about how to use marker but only described how to use Gmap's own marker icon, and here's how to customize the marker icon.

We first need to get the location information in the trigger function of the mouse click event, and then we start customizing the drawing marker icon, and when I describe the use of marker in the previous section, I use the Gmarker subclass Gmarkergoogle created Marker object, Careful child shoes will find that Gmarkergoogle has a constructor with two parameters, and the first pointlatlng type argument is undoubtedly used to indicate the position of marker on the map, and the second parameter is an object of bitmap type. It is clear that this bitmap parameter can be used to construct the custom marker, so we first need to provide a bitmap object to represent the marker icon. The author is lazy, so do a black long width of 50 rectangle (as for bitmap on how to draw graphics, please Baidu). Then construct the Marker object and add it to our overlay layer.

          POINTLATLNG p= gmapcontrol1.fromlocaltolatlng (e.x, e.y);
          Bitmap Bitmap = new Bitmap (50,50);
          Graphics g = graphics.fromimage (bitmap);
          G.clear (color.black);
          Gmapmarker marker = new Gmarkergoogle (P, bitmap);
          THIS.OVERLAY.MARKERS.ADD (marker);

Click on the map we can see the mouse click on the place to create a black rectangle. The points on the map correspond to the midpoint of the bottom boundary of the rectangle. Rule Graphic

The last time I introduced Gmapoverlay, I always wanted to make a drawing with the mouse to implement the rule drawing on the map. Here the author describes the method of drawing a circle with the mouse drag to draw the rectangle.

First we want to capture Gmap mouse down events, mouse movement events, and mouse lift events. This allows us to start drawing when the mouse is pressed, specifying the area to draw when the mouse is moved, and ending drawing when the mouse is raised. So let's first declare a Boolean type to distinguish whether the mouse is pressed, when pressed to true, false when raised, and where the mouse is pressed when the mouse is pressed. Note that when you compute the boundary point of a geometry in a mouse movement event, if you can drag and drop the map at this time, then after we mouse click map will follow our mouse to start moving, so we are in the mouse movement event is always we press the latitude and longitude of the coordinates point, So we need to stop the mouse from dragging the map when we're drawing the regular graphics.

        BOOL Mousepressflag = false;
Draw a rectangle

If we want to draw a rectangle, then in the mouse movement event in conjunction with the mouse position through the mathematical analysis of the Four corners of the rectangle latitude and longitude coordinates, converted to four corner coordinates after the use of gmapoverlay polygons to draw the coordinates point.

        void Drawrectanglecenter () {overlay.
            Polygons.clear (); List.
            Clear (); if (nowpoint. Lat < Presspoint. Lat) {if (nowpoint). Lng < Presspoint. Lng) {Double lat = presspoint. Lat-nowpoint.
                    Lat; Double LNG = presspoint. Lng-nowpoint.
                    Lng; List. ADD (New Pointlatlng (Presspoint). Lat+lat, Nowpoint.
                    Lng)); List. ADD (New Pointlatlng (Presspoint). Lat+lat,presspoint.
                    LNG+LNG)); List. ADD (New Pointlatlng (Nowpoint). Lat, Presspoint.
                    LNG+LNG)); List.
                ADD (Nowpoint); else{Double lat = presspoint. Lat-nowpoint.
                    Lat; Double LNG = -1*presspoint. Lng + nowpoint.
                    Lng; List. ADD (New Pointlatlng (Presspoint). Lat+lat,presspoint.
                    LNG-LNG)); List. ADD (New Pointlatlng (Presspoint). Lat+lat, Nowpoint.
                    Lng)); List.
               ADD (Nowpoint);     List. ADD (New Pointlatlng (Nowpoint). Lat, Presspoint.
                LNG-LNG)); }}else{if (Nowpoint. Lng < Presspoint. Lng) {Double lat = -1*presspoint. Lat + nowpoint.
                    Lat; Double LNG = presspoint. Lng-nowpoint.
                    Lng; List.
                    ADD (Nowpoint); List. ADD (New Pointlatlng (Nowpoint). Lat, Presspoint.
                    LNG+LNG)); List. ADD (New Pointlatlng (Presspoint). Lat-lat,presspoint.
                    LNG+LNG)); List. ADD (New Pointlatlng (Presspoint). Lat-lat, Nowpoint.
                Lng)); }else {Double lat = -1* presspoint. Lat + nowpoint.
                    Lat; Double LNG = -1*presspoint. Lng + nowpoint.

                    Lng; List. ADD (New Pointlatlng (Nowpoint). Lat, Presspoint.
                    LNG-LNG)); List.
                    ADD (Nowpoint); List. ADD (New Pointlatlng (Presspoint). Lat-lat, Nowpoint.
                    Lng)); List. ADD (New Pointlatlng (Presspoint). lat-Lat,presspoint.
                LNG-LNG));
            } Gmappolygon polygon = new Gmappolygon (list, "polygon"); Polygon.
            Fill = new SolidBrush (Color.FromArgb (color.red)); Polygon.
            Stroke = new Pen (Color.Blue, 2); Polygon.
            IsHitTestVisible = true; Overlay.
        Polygons.add (Polygon); }

The author of the above code is relatively stupid, but we can use the mouse to drag draw a rectangle, here is the mouse to press the point as the center of the rectangle, moving the point as Four corners of one point to draw.
Draw Oval

The circle is different from the drawing of the rectangle, the rectangle is the number of fixed vertices, and the vertex of the circle, if we do by way of differential, it will be very troublesome, then use marker to draw the circle. The marker is actually made to make the original custom bitmap background transparent and then draw an ellipse on the bitmap.

        void Drawcircleradius () {this.overlay.Markers.Clear ();
            Gpoint Pnow = this.gMapControl1.FromLatLngToLocal (nowpoint);
            Gpoint ppress = this.gMapControl1.FromLatLngToLocal (presspoint); Double width = math.abs (pnow. X-ppress.
            X); Double height = math.abs (pnow. Y-ppress.
            Y);
            if (Width < | | height <) return;
            Bitmap bitmapmarker = new Bitmap (int) width, (int) height);
            Graphics Graphicsmarker = Graphics.fromimage (Bitmapmarker);
            Graphicsmarker.clear (color.transparent);
            Graphicsmarker.drawellipse (New Pen (color.red,2), new Rectangle (0, 0, bitmapmarker.width-1, bitmapmarker.height-1)); POINTLATLNG p = this.gMapControl1.FromLocalToLatLng ((int) (Pnow). X + ppress. X)/2, (int) (Pnow. Y>ppress. Y? Pnow. Y:ppress.
            Y));
            Gmapmarker marker = new Gmarkergoogle (p, bitmapmarker);
        THIS.OVERLAY.MARKERS.ADD (marker); }

The place to note is to use fromlatlngtolocal to convert the latitude and longitude coordinates to pixel coordinates, to create the corresponding long width of the bitmap object, the drawing effect is as follows:

Finally attach code http://download.csdn.net/detail/yuanquanzheng/9601719

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.