Sharpmap Learning (3)

Source: Internet
Author: User

Learn every day and make progress every day.

I want to provide some help for beginners. For the experts who have thoroughly studied sharpmap and GIS technology, please do not hesitate to give us more guidance for these cainiao.

Today, we will talk about the basic usage skills of sharpmap. We will use attribute to fill in the color of map objects, so that users can see more clearly the representation of key business objects on the map, and how to customize the display content and font size of the label layer. Therefore, today's themes are mainly custom: custom theme, custom label, and label font.

 

First, we need to fill the map with different colors to make them look colorful and easy to distinguish. For example, rivers and lakes should be filled in blue, lawns should be filled in green, houses should be filled in white, roads should be filled with blue, and so on. How can this problem be solved? It's easy. Let's take a look at it first.Code:

Sharpmap. Rendering. thematics. customtheme itheme = new sharpmap. Rendering. thematics. customtheme (getmystyle );

Add the above line of code when initializing the map. It defines a custom theme object. The constructor of this object needs to input a method (delegate) that we write by ourselves. This method details how this theme is defined, the method code is as follows:

 

Code
  Private   Static Sharpmap. Styles. vectorstyle getmystyle (sharpmap. Data. featuredatarow row)
{
Sharpmap. Styles. vectorstyle Style =   New Sharpmap. Styles. vectorstyle ();
Switch (Row [ " Status " ]. Tostring (). tolower ())
{
Case   " Vailable " : // If status is interred, fill it with yellow
Style. Fill = Brushes. Yellow;
Return Style;
Default :
Style. Fill = Brushes. Green;
Return Style;
}
}

 

You can understand this simple code. This delegate definition requires a featuredatarow. Its function is to process each object during map initialization, in this method, we need to check the value of feature to determine the specific colors to fill. Here, when the status is available, it is filled with yellow; otherwise, it is filled with green. The defined vectorstyle is used to return to the map. Is it easy?

After the map is filled up, we need to show what they are. In the first article, we can display an attribute of an object. However, when a map is expanded to a certain proportion, only the name information is insufficient to meet users' needs. For example, when we look at a house from a distance, we only need to see its color. But when we are very close, do we see all white? Obviously not. We can also see the house number, the sticker on the window, and other details. The same is true for maps. When the scale is changed, more detailed information should be listed. Old Rules: paste the code first:

Sharpmap. layers. labellayer mylabel = new sharpmap. layers. labellayer ("labels ");
Mylabel. datasource = mylayer. datasource;

Mylabel. labelstringdelegate = new sharpmap. layers. labellayer. getlabelmethod (getlabelstring );

Here, we first define a label (just like in the initial map or just in the initialized map ), then, assign the data source of the layer you want to display to the data source of the label, and specify a method for labelstringdelegate of the label layer (this method passes in a featuredatarow and returns a string ), finally, the Code is as follows:

Code
  Public   Static   String Getlabelstring (featuredatarow Dr)
{
String Field1 =   String . Empty;
String Field2 =   String . Empty;
String Field13 =   String . Empty;

If (Dr [ " Housenumber " ] ! =   Null )
{
Field1 = Dr [ " Housenumber " ]. Tostring ();
}
If (Dr [ " Hostname " ] ! =   Null )
{
Field2 = Dr [ " Hostname " ]. Tostring ();
}
If (Dr [ " Memo " ] ! =   Null )
{
Field3 = Dr [ " Memo " ]. Tostring ();
}
Stringbuilder sb =   New Stringbuilder ();
SB. append (field1 );
SB. append ( " : " );
SB. append (field2 );

SB. append ("\ R \ n" +Field3 );

ReturnSB. tostring ();
}

The code is too simple to be detailed. It is worth noting that if you want to wrap a line, use "\ r \ n", which does not support HTML strings. Haha, solve another problem. Good?

Then, the new problem arises. The font size is always so big or small. What should I do? It looks quite uncomfortable. If the font size changes with the scale. This is okay. We'll fix it right away. Check the Code:

 

Code
Public   Void Changefontsize ( Double Zoom, sharpmap. layers. ilayer)
{
Sharpmap. layers. labellayer = (Sharpmap. layers. labellayer) ilayer;
If (Zoom <   800   && Zoom >   400 )
{
(Sharpmap. layers. labellayer). style. Font =   New System. Drawing. Font (system. Drawing. fontfamily. genericserif, 6 );
}
Else   If (Zoom <   400   && Zoom >   140 )
{
(Sharpmap. layers. labellayer). style. Font =   New System. Drawing. Font (system. Drawing. fontfamily. genericserif, 8 );
}
Else   If (Zoom <   140.00   && Zoom >   20 )
{
(Sharpmap. layers. labellayer). style. Font =   New System. Drawing. Font (system. Drawing. fontfamily. genericserif, 16 );
}
Else   If (Zoom <   20.00 )
{
(Sharpmap. layers. labellayer). style. Font =   New System. Drawing. Font (system. Drawing. fontfamily. genericserif, 48 );
}
Else
{
(Sharpmap. layers. labellayer). style. Font =   New System. Drawing. Font (system. Drawing. fontfamily. genericserif, 70 );
}
}

This method imports a zoom value and an ilayer. If there is no return value, you can directly modify the ilayer.

I believe everyone can understand it? It is worth noting that the map object on ajaxmap has a property called zoomamount, which must be specified at the front-end for JavaScript attributes. It indicates the scale-up or scale-down of the map. Demo is specified in the listitem of radiobuttonlist. This process is like this: When you click the zoom in button, a javascript code is automatically executed:

Ajaxmapobj. disableclickevent (); ajaxmapobj. zoomamount = 3;

Then, when we click the map, the click event is disabled, and the system refreshes the map, the new map parameters (such as Zoom) are received in handler, and the map is re-drawn. Therefore, we get the zoom parameter in handler and use the getlayerbyname ("") method to obtain the label layer.

Changefontsize (zoom, map. getlayerbyname ("mylabels "));

In this way, the font changes with the scale. Some may ask: how can the label content change with the scale? Just put the sentence

Mylabel. labelstringdelegate = new sharpmap. layers. labellayer. getlabelmethod (getlabelstring );

Put it in the corresponding if... In the else statement. Simple?

 

This is today.

These are tips, which may not be worth mentioning in the eyes of Daniel, but I hope to help beginners and hope that everyone can enjoy their work and enjoy a good mood every day.

Finally, you are welcome to shoot bricks.

 

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.