Google map controls implement GMAP's custom graphics system through extension

Source: Internet
Author: User
Tags custom graphics

To develop a network planning and optimization system over the past two days, you need to develop a set of maps, place custom icons on the maps, and place complex textures, or draw rectangular polygon to move and shrink the maps. It was very painful at the beginning, through GMAP.. NET development, and use the drawtools tool above the CodeProject (if you are interested, you can search for it by yourself, and there are related articles on Blog), but you cannot move with the map, it seems to be floating on the map. Finally, this method can only be abandoned.

After reading the GMAP discussed in several blogs, we have discussed other aspects, that is, there is no way to customize the map. Later, I repeatedly studied the structure and routine of the entire GMAP. NET class, and finally found the method.

First of all, to display icons and graphs, you must have a layer concept, as if a layer is attached to a map. It can be represented as the following four layers from bottom to top:

Geographic information layer-complex texture layer-landmark layer-user data operation layer

Simply put, a map is displayed on the "geographic information layer" and "complex texture layer" to map, which can be scaled down and displaced. A custom icon can be placed on the "landmark layer, "user operation layer" can define other special features.

(1) Implement GMAPMARKER for custom icons

We know that there is a Gmapmarker class in GMAP, which can place icons on the map, which can be moved but cannot be scaled down. Only one balloon is really ugly, how can we design custom icons? This class can be derived. GMapMarkerBS is a base station (BS) Class I have established. You can draw a base station on a map.

Public class GMapMarkerBS: GMapMarker
{
Bitmap myBitmap; // stores the icon information to be saved.
Public int ID; // mark the current class

Public GMapMarkerBS (PointLatLng p, int thisID)
: Base (p)
{
MyBitmap = (Bitmap) Bitmap. FromFile (Application. StartupPath + "\ Cursor \ BS_Large.png ");
ID = thisID;
ToolTipText = "Test Base Station" + ID. ToString ();
ToolTipMode = MarkerTooltipMode. OnMouseOver;

Size = myBitmap. Size;
Offset = new System. Drawing. Point (-Size. Width/2,-Size. Height/2 );



}
Public GMapMarkerBS (PointLatLng p, Bitmap thisBitmap, int thisID)
: Base (p)
{
ID = thisID;
ToolTipText = "Test Base Station" + ID. ToString ();
ToolTipMode = MarkerTooltipMode. OnMouseOver;
MyBitmap = thisBitmap;

Size = myBitmap. Size;
Offset = new System. Drawing. Point (-Size. Width/2,-Size. Height/2 );

}

Public override void OnRender (Graphics g)
{
G. DrawImage (myBitmap, new System. Drawing. Rectangle (LocalPosition. X, LocalPosition. Y, 80,100 ));

}
}

The two constructors can implement default icons and custom icons. The key is to reload the Onrender () function, which can be understood at a glance.

Both Size and Offset are defined in the base class Gmapmarker, which indicates the Size and Offset of the current icon. If the Size is not set, you can draw images on the map, but you cannot make it trigger the "click" event! Note that it sets a valid operation range on the map, rather than the drawing size. The so-called offset should be the offset between the click and the upper left corner of the icon.

How can I draw this icon on a map?

GMapMarkerBS tempMarker = new GMapMarkerBS (thisPosition, BSIDCounter );
GISObjectOverlay. Markers. Add (tempMarker );

GISObjectOverlay is a layer that I have created,

GISObjectOverlay = new GMapOverlay (myGMapControl, "GISOverlay ");

In this way, you can draw a custom icon graph on the map.

(2) Implement polygon drawing

Implement polygon and fill in the map, and also implement scaling and movement. If you do not inherit the built-in class of GMAP, it will be exhausted. Fortunately, GMAP provides support.

The implementation idea is like this. After you select the polygon drawing tool, first draw a point in time on the temporary Drawing Layer GISTempOverlay, use the built-in cross icon to represent it, and then double-click to complete a polygon Drawing operation, transmits the currently stored LIST table to the GMAP class.

Private void MainMap_MouseDown (object sender, MouseEventArgs e)
{

Case GISObjectType. Polygon:

GISTempPoint. Add (MainMap. FromLocalToLatLng (e. X, e. Y ));
GISTempOverlay. Markers. Add (tempMarker );
Break;

}

The above code implements the left-click function to draw the time point on the map;

Private void MainMap_MouseDoubleClick (object sender, MouseEventArgs e)
{

Case GISObjectType. Polygon:
If (GISTempPoint. Count <3)
{
MessageBox. Show ("undrawn surrounding image ");
Break;
}
Else
{
MyGISObject. AddRouteObject (GISTempPoint, "Polygon ");
GISTempOverlay. Markers. Clear ();
GISTempPoint. Clear ();
}
Break;

}

The AddRouteObject () function is a path class function written by myself. The Code is as follows:

Public void AddRouteObject (List <PointLatLng> myPositionList, string name)
{
GISRouteObjectList. Add (new RouteObject (myPositionList, name, RouteIDCounter ));
GMap. NET. WindowsForms. GMapPolygon tempPolygon = new GMap. NET. WindowsForms. GMapPolygon (myPositionList, name );
GISObjectOverlay. Polygons. Add (tempPolygon );
}

In this way, polygon is drawn.

(3) Designing complex textures

The icon cannot scale down with the map. Isn't it possible to scale down with a picture attached? Yes.

You can generate a PNG Image to ensure transparency. Create a new layer named GISPictOverlay to store the Texture type. Reload gmapmarker to get GMapMarkerPict and add the variable zoom,

Write this in the OnRender () function:

G. DrawImage (myBitmap, new System. Drawing. Rectangle (LocalPosition. X, LocalPosition. Y, myBitmap. Width * zoom, myBitmap. Height * zoom ));

Outside the class, when the map zoom level changes, modify the static variable zoom of this class. Note! This method can only change the size of your image as the map is scaled down, but it is not stuck... because every time the scale-down level of a map changes, it seems that the map size changes to the power of 2, but this is linear. I have to study how to set it. It seems that this link is provided in the GMAP project file.

Of course, we also need to store all the data on the current map, such as polygon, icons, textures, etc. We can set the data structure on our own. I have little knowledge in this regard and I am afraid to give it a try. I wrote an article in the blog for the first time, and I am a. NET cainiao. When I saw no GMAP Drawing Problem on the internet, I took my own case to discuss it. The Grand chivalrous guys pat on me. I also want to ask the weak question. Why is there no decent development documentation for such a large open-source project as GMAP? Welcome to the discussion!

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.