Skip to main content

Windows 8 – XAML/C# how to Add multi push pins to map control Bing maps


Greetings readers I promised you in my previous post that I am gonna go a little bit deeper in the Bing map control and I will should more advanced scenarios and one is push pins,
Basically I am going to tell you how you can add push pin to your map and also how to dynamically populate those push pins using binding

Refer to my previous article Using bing maps part one for more details cause I am going build on it so if you ever feel that you don’t understand something just click the link and spend a few minutes reading it, I promise you it will prove worthy.

First lets add a normal push pin to our map 

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <bm:Map x:Name="MyMap" Width="640" Height="480" 
              Credentials="{StaticResource BingMapsApiKey}" >
            <bm:Map.Children>
                <bm:Pushpin x:Name="pushPin1" />
            </bm:Map.Children>
        </bm:Map>

    </Grid>


Check the code sample above describing how to add a push pin to the map
Below is showing the default position of the push pin



If we want to set the location we place the code below, I will put it on the onnavigate event
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.         ///  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                MapLayer.SetPosition(pushPin1, new Location(12f, 14f));
            }
            catch (Exception ex)
            {
                var x = ex.Message;
                throw;
            }
            base.OnNavigatedTo(e);
        }
Now let’s see how it is going to look like


 Looks good so far now what happen if we wanted to add more push pins to the map, well we would probably copy and paste the push pin tag as many as we wanted but what if we needed to dynamic this part, suppose that we have a list of locations and I want to dynamically create as many pushpins as there is in this list
The answer is simple databinding and data template
First I am gonna add the pushpin as map item template in the resource section
<DataTemplate x:Key="LogoTemplate">
            <bm:Pushpin IsTapEnabled="True" Background="#FFB6DE2E" >
                <bm:MapLayer.Position> 
                    <bm:Location Latitude="{Binding Latitude}" Longitude="{Binding Longitude}" />
                </bm:MapLayer.Position>
            </bm:Pushpin>
        </DataTemplate>


Then I am going to add this to the Bing map control
Like the code below
<bm:Map x:Name="MyMap" Width="640" Height="480" 
              Credentials="{StaticResource BingMapsApiKey}" >
            <bm:MapItemsControl ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding PushpinCollection}" >
            </bm:MapItemsControl>
        </bm:Map>



Now let’s prepare the data-bind collection we are going to work on and it has to be an observable collection of a custom type pushpin
Here is the definition for pushpin class

public class PushpinModel
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
    }

And lets add the
fillings for this collection in the constructor here in the code below


public ObservableCollection<PushpinModel> PushpinCollection { get; set; }

        public MainPage()
        {
            this.InitializeComponent();
            PushpinCollection = new ObservableCollection<PushpinModel>();
            PushpinCollection.Add(new PushpinModel(){Latitude = 14f,Longitude = 15f});
            PushpinCollection.Add(new PushpinModel() { Latitude = 14f, Longitude = 15f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 10f, Longitude = 15f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 14f, Longitude = 11f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 13f, Longitude = 15f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 19f, Longitude = 15f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 14f, Longitude = 10f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 13f, Longitude = 15f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 15f, Longitude = 17f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 20f, Longitude = 19f });
            PushpinCollection.Add(new PushpinModel() { Latitude = 18f, Longitude = 18f });
            DataContext = this;

        }

Now run and check the output screen below
it works just as that u have multi pushpins dynamically created using binding
thanks for reading

Comments

  1. Great article and very informative thanks dapfor. com

    ReplyDelete
    Replies
    1. thanks i am glad i was of a help, and thanks for the praise again :)

      Delete


  2. Why I have namespace error in this line?

    PS. Very nice article.

    ReplyDelete
    Replies
    1. Which line exactly are we talking about, any how I will upload the source code if possible if not please refer to which line

      Delete
  3. you are adding the pushpin as map item template in the resource section. kindly define "resource section"??

    ReplyDelete
  4. for Xaml and element can have his own resources for instance i could use
    for windows phone page is what i ment .. would look like

    </phone.appplicationpage.Resources

    Hope this helps

    ReplyDelete

Post a Comment

Popular posts from this blog

(AsyncWebClient) Async Webclient for windows phone 8

Using windows phone web client is pretty common but the thing is you can not use it using Async -> Await mechanism so i used threading to create an async functionality for The Download string and upload string methods here is the code below // Comment public class AsyncWebClient { public Task DownloadString(Uri uri) { var task = new TaskCompletionSource(); try { var client = new WebClient(); client.DownloadStringCompleted += (s, e) => { task.SetResult(e.Result); }; client.DownloadStringAsync(uri); } catch (Exception ex) { task.SetException(ex); } return task.Task; } public Task UploadString(Uri uri, string content) { var task = new TaskCompletionSource(); try { var client…

RuntimeBinderException cannot resolver property in Unit Test in case of dynamic return.. Solved

Xamarin UI Tests – Deep dive Part 1

IntroAs the title may describe this is the first of the series of articles that will cover the UI Test of Xamarin in a deep dive, we will start simple and then dig deeper as we go. Since this is very well the first of the series, it will mostly cover up the architecture and the testing technique that Xamarin UI Tests uses.

The Technology
Xamarin UI Test is an automation testing framework similar to selenium, Watir, Watin (.net), Robot and Sikuli, if you have used BDD (Behavioral Driven Development) or a more advanced TDD (Test Driven Development) approach at least you must have come across one of these frameworks, and in that perspective comes Xamarin UI Testing as an automation framework designed specifically for Xamarin Automation testing, For starter Xamarin UI Tests was not build from scratch, instead it was built on top of another UI Automation testing framework that is targeting android and iOS sepecifically which is Calabash, I have to say that this choice was made wisely as of n…