Add Map and Points to Windows Phone 7

Here are the steps to add a map and point on the map.

1) Add a reference to Microsoft.Phone.Controls.Maps

2) Drag a ‘Map’ Windows Phone Control to the Page xaml.

3) Register on Bing Maps Portal and obtain an ID or Key.  Without this the map will display not register message.

4) Add the key obtained above to the xaml code.  Modify the xaml created by dragging the control and dropping it on the page.

    <Microsoft_Phone_Controls_Maps:Map    Name="map1" VerticalAlignment="Top" >     
            <Microsoft_Phone_Controls_Maps:Map.CredentialsProvider>
                <Microsoft_Phone_Controls_Maps:ApplicationIdCredentialsProvider ApplicationId="your key here" />
            </Microsoft_Phone_Controls_Maps:Map.CredentialsProvider>
    </Microsoft_Phone_Controls_Maps:Map>

 

5) Go to  the code behind

6) Add a using statement to reference the map control

using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;

7)  Add an instance variable for the page

MapLayer PinLayer1;

8) Create a pin and location

Pushpin MyPin = new Pushpin();
MyPin.Location = new Location();

9)  Set the Lat and Lon for the location.

MyPin.Location.Latitude = 27.96206353406851;
MyPin.Location.Longitude = -82.44960308074951;

10) Add a label to the Pin

MyPin.Content = "KForce";

11) Create a Map Layer

PinLayer1 = new MapLayer();

12) Add  Pin to Layer

PinLayer1.Children.Add(MyPin);

13) Add Layer to map

map1.Children.Add(PinLayer1);

14)  Center and Zoom on the location

map1.Center = MyPin.Location;
map1.ZoomLevel = 9;

Happy Coding

Advertisements