Discussion on Windows Phone 7 development on the 31st day-20th: map control

Source: Internet
Author: User
This article is the 20th day of the "Windows Phone 7 development on the 31st day" series.

Yesterday's long message was about how to use push notifications to send data to your mobile phone, even ifProgramNot running. Today, let's go back to the control. More specifically, it's a map control. Nowadays, the geo-positioning service is becoming more and more popular in mobile applications. It is more and more important to tell users where they are and what they are around.

Use Map controls

As part of the Visual Studio 2010 toolbox, you only need to drag a map control to the page. In this case, you will notice that another XML namespace is added to the page.

Xmlns: Map = "CLR-namespace: Microsoft. Phone. Controls. Maps; Assembly = Microsoft. Phone. Controls. Maps"

This is the default added XAML in my example (after I adjusted the position and size ):

< Map: Map Height = "607" Horizontalalignment = "Left" Name = "Mymap" Verticalalignment = "TOP" Width = "456"   />

Finally, let's look at the map in a program:

You will notice that the white text in the center says "invalid certificate. Register a developer account ". For the rest of this article, I will talk about all the operations we can perform on this map control, including obtaining an effective developer API key.

Create your developer account

The first thing you need to do before building your map program is to obtain a bing map API key. This is simple and free, and you can remove the ugly white lines above. To get it, you need to go to the Bing map site and register it. After that, you need to create an API key. The form looks like this:

After you fill in, they will give you an API key that looks like this:

AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9

(This is not my API key. I have replaced many characters. But it looks like what you see .)

Use the credentials provider attribute

Now that you already have an API key, we need to insert it into our program. If your program has only one map control, it is very good to use it like this below:

< Map: Map Credentialsprovider = "AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9" >

If you want to reuse this value, save it to another place, such as the app. XAML file. See the following example. You can use it in the app. XAML file and the actual map control.Code. We created a static credentialsprovider in the app. XAML file and accessed it on our page.

App. XAML

Code < Application. Resources >
< Map: applicationidcredentialsprovider Applicationid = "AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9" X: Key = "Bingmapsapikey" > </ Map: applicationidcredentialsprovider >
</ Application. Resources >

Map Control <Map: MapCredentialsprovider="{Staticresource bingmapsapikey}">

Changing the features of map controls

There are many options to change the appearance of the map. For example, you can change the road mode to the air mode or decide whether to display the zoom level selector. You have many options that can be set. They are all in Bing Maps Silverlight control interactive SDK. I don't have to list all the options here (they have done a lot of work), but I want to tell you how to get your data from the map. There are only two things to focus on: adding a map pin and a custom shape to the map.

Add a map to the map

In C #, you can create a pushpin object, set its position, and add it to the map. It can also be implemented in XAML. Obviously, XAML provides you with a quicker method, but it is not complicated.

XAML

< Map: pushpin Location = "40.1449,-82.9754" Fontsize = "30" Background = "Orange" Content = "1"   />

C #

Code Pushpin =   New Pushpin ();
Location =   New Location ();
Location. Latitude =   40.1449 ;
Location. longpolling =   - 82.9754 ;
Pushpin. Location = Location;
Pushpin. Background =   New Solidcolorbrush (colors. Orange );
Pushpin. Content =   " 1 " ;
Pushpin. fontsize =   30 ;
Mapcontrol. Children. Add (pushpin );

In the above example, a graph is pinned to my office in either way, and can be found in 8800 Lyra drive, Columbus. It looks like this in my program:

If you want to know how to convert your address to latitude and longitude, please refer toArticle. It contains the geographic address code and the content you want to do in the mobile phone program.

Add custom XAML to a map

In the map program set, there is a small control called mappolygon. Providing it with a series of positions, it will draw a custom polygon in your map, and it will still be fixed at that position when the user scales and moves the map. Because it is bound to a map based on longitude and latitude, it is necessary in the program to easily use it to outline the outlines of a home, State, region, or even a parking lot. The implementation method is as follows:

XAML

< Map: mappolygon Fill = "Purple" Stroke = "White" Opacity = ". 7" Locations = "40.1449,-82.9754 40.1449,-12.9754 10.1449,-82.9754"   />

C #

Code Mappolygon =   New Mappolygon ();
Mappolygon. Fill =   New Solidcolorbrush (colors. Purple );
Mappolygon. Stroke =   New Solidcolorbrush (colors. White );
Mappolygon. Opacity = . 7 ;
Locationcollection locations =   New Locationcollection ();
Location =   New Location ();
Location. Latitude =   40.1449 ;
Location. longpolling =   - 82.9754 ;
Location location1 =   New Location ();
Location1.latitude =   40.1449 ;
Location1.longpolling =   - 12.9754 ;
Location location2 =   New Location ();
Location1.latitude =   10.1449 ;
Location1.longpolling =   - 82.9754 ;
Locations. Add (location );
Locations. Add (location1 );
Locations. Add (location2 );
Mappolygon. Locations = Locations;
Mapcontrol. Children. Add (mappolygon );

That's all. We have covered a map with a dingtalk and a custom polygon. For more functions of this control, see Bing Maps Silverlight control interactive SDK (below is the mappolygon)

Download Sample Code

In this example, you can find an example of using XAML and C # To add the map nails and polygon to the map. Maybe you don't need all the content. You can take one of them. It's up to you to decide.

Address: http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-20-Map-Control.aspx

If you like my article, click "recommendation ", Thank you!

 

Related Article

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.