D Lng

D Lng

  • NA
  • 12
  • 4.9k

Locate Me on map error

Jan 21 2012 11:03 AM
Hey Guys,

I have integrated a BING map onto my app, and it works okay, can see what I need to see.
My issue is when I want to add a push-pin or highlight where I am on the map, it returns an error when i select the Locate Me button.

It errors out on my .xaml.cs on the line
mapControl.Items.Add(p);
and the details : {"Operation not supported on read-only collection."}

Ill post the xaml.cs code below, if anyone can suggest a fix..only thing that's bugging me now.

using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using System.IO;


namespace MySchoolApp
{
    public partial class MapPage : PhoneApplicationPage
    {


        public MapPage()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            map.CredentialsProvider = new ApplicationIdCredentialsProvider(App.ViewModel.Settings.BingMapsKey);
        }
        private GeoCoordinateWatcher loc = null;


        private void meButton_Click(object sender, RoutedEventArgs e)
        {
            if (loc == null)
            {
                loc = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                loc.StatusChanged += loc_StatusChanged;
            }
            if (loc.Status == GeoPositionStatus.Disabled)
            {
                loc.StatusChanged -= loc_StatusChanged;
                MessageBox.Show("Location services must be enabled on your phone.");
                return;
            }
            loc.Start();
        }


        void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {


            if (e.Status == GeoPositionStatus.Ready)
            {
                Pushpin p = new Pushpin();
                p.Template = this.Resources["pinMyLoc"] as ControlTemplate;
                p.Location = loc.Position.Location;
                mapControl.Items.Add(p);
                map.SetView(loc.Position.Location, 17.0);
                loc.Stop();
            }
        }
    }
}