Best way to extend Bing Maps for Metro functionality

Answered Best way to extend Bing Maps for Metro functionality

  • Sunday, July 08, 2012 6:12 PM
     
      Has Code

    Hi, I am looking to extend the functionality of the Bing Maps for Metro control.  In particular, I would like to add:

    • Support for collections of Pushpins, i.e. if there are thousands, only add those 'in view' to the map layer.
    • Support for 'callouts' to appear when pushpins are clicked.

    To accomplish this, I tried sublassing Map, but you have sealed the class - is this intentional?  Therefore, what is the best way to extend the functionality?  I have begun something like the code below; is this a good way to go?  Or do you foresee problems with this approach?

        public class MapManager
        {
    
            // Private Members
            private Map map;
            AnnotationManager annotationManager;
    
            public MapManager(Map _map)
            {
                this.map = _map;
                annotationManager = new AnnotationManager(this.map);
            }
    
            public void addAnnotations(IEnumerable<IMapAnnotation> annotations)
            {
                annotationManager.addAnnotations(annotations);
            }
            public void removeAnnotations(IEnumerable<IMapAnnotation> annotations)
            {
                annotationManager.removeAnnotations(annotations);
            }
        }
    
    
    
    
    
    internal class AnnotationManager
        {
    
            // Private members
            Map map;
            MapLayer annotationsLayer;
            Dictionary<IMapAnnotation, Pushpin> pins;
            AnnotationCollection annotationCollection;
    
            public AnnotationManager(Map map)
            {
                // Map
                this.map = map;
    
                // Collection to hold the annotation objects
                annotationCollection = new AnnotationCollection();
                annotationCollection.AnnotationsChanged += annotationCollection_AnnotationsChanged;
    
                // Layer to hold the annotations
                annotationsLayer = new MapLayer();
                map.Children.Add(annotationsLayer);
    
                // Pushpins to represent the annotations
                pins = new Dictionary<IMapAnnotation, Pushpin>();
            }
    
            // Public Methods
            public void addAnnotations(IEnumerable<IMapAnnotation> annotations)
            {
                annotationCollection.addAnnotations(annotations);
            }
            public void removeAnnotations(IEnumerable<IMapAnnotation> annotations)
            {
                annotationCollection.addAnnotations(annotations);
            }
    
            // Incoming event - Annotations Changed
            void annotationCollection_AnnotationsChanged(object sender, AnnotationCollectionChangedEventArgs e)
            {
                foreach (IMapAnnotation item in e.annotationsAdded)
                {
                    Pushpin newPin = new Pushpin();
                    newPin.Text = item.Title();
    
                    Bing.Maps.Location loc = new Bing.Maps.Location(item.Latitude(), item.Longitude() );
                    MapLayer.SetPosition(newPin, loc);
    
                    pins.Add(item, newPin);
                    annotationsLayer.Children.Add(newPin);
                }
                foreach (IMapAnnotation item in e.annotationsRemoved)
                {
                    Pushpin oldPin;
                    if (pins.TryGetValue(item, out oldPin))
                    {
                        annotationsLayer.Children.Remove(oldPin);
                        pins.Remove(item);
                    }
                }
            }
    
    
    
    
    
        public interface IMapAnnotation
        {
            string Title() ;
            string Subtitle();
            double Longitude();
            double Latitude();
    
        }
    


    Comments very welcome.


    _______________________________ Carl Partridge

All Replies