Best way to extend Bing Maps for Metro functionality
-
Sunday, July 08, 2012 6:12 PM
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
-
Monday, July 09, 2012 7:27 AM
Hi Carl,
I'm not an expert on the topic and experts can respond better but there was a similar discussion underway on the same forum regarding clustering of pushpin. This might help (to show only handful of puspins at a particular instant),Also, if I understand correctly any user control can be used in place of pushpin which means you can have a user defined control encapsulating the logic of custom popup.
- Marked As Answer by Richard_BrundrittMicrosoft Employee, Owner Friday, July 13, 2012 8:03 AM

