locked
Add pushpins to the map on run time. RRS feed

  • Question

  • I am using the below code to create a map object, and then using the dispatcher thread to add pushpins on the map. When my window loads, it frozen until the dispatcher finishes executing. I am trying to fix this issue with no luck. Any help would be appreciated. 

    public MainWindow()
            {        
                InitializeComponent();

                myMap.Center = new Microsoft.Maps.MapControl.WPF.Location(43.016893, -85.726236);
                myMap.ZoomLevel = 14;
                myMap.CredentialsProvider = new ApplicationIdCredentialsProvider(BingMapsKey);
                MainGrid.Children.Add(myMap);

                myMap.Dispatcher.BeginInvoke(new Action(() =>
                {
                    List<string> temp = new List<string>()
                {
                    "2929 Walker Ave NW, Grand Rapids, MI 49544",
                    "3175 ROCHESTER RD.,ROCHESTER HILLS,MI,USA",
                    "3651 TOWNE BLVD.,FRANKLIN,OH,USA",
                    "5555 CLEVELAND AVE.,COLUMBUS,OH,USA",
                    "30800 LITTLE MACK ROAD,ROSEVILLE,MI,USA",
                    "3145 ANN ARBOR/SALIN RD.,ANN ARBOR,MI,USA",
                    "1500 HILLCREST AVE.,SPRINGFIELD,OH,USA",
                    "37201 WARREN ROAD,WESTLAND,MI,USA",
                    "22600 ALLEN ROAD,WOODHAVEN,MI,USA",
                    "2777 AIRPORT RD.,JACKSON,MI,USA",
                    "12821 CROSSOVER DR.,DEWITT,MI,USA",
                    "25225 SCHOENHERR RD,WARREN,MI,USA",
                    "137 ELLIOTT RD,DEFIANCE,OH,USA",
                    "855 S RANDALL ROAD,ST. CHARLES,IL,USA"
                };

                    foreach (string s in temp)
                    {
                        string geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + s + "?o=xml&key=" + BingMapsKey;
                        XmlDocument geocodeResponse = GetXmlResponse(geocodeRequest);

                        if (geocodeResponse != null)
                        {
                            XmlNamespaceManager nsmgr = new XmlNamespaceManager(geocodeResponse.NameTable);
                            nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/v1");

                            XmlNodeList locationElements = geocodeResponse.SelectNodes("//rest:Location", nsmgr);

                            if (locationElements.Count != 0)
                            {
                                XmlNodeList displayGeocodePoints = locationElements[0].SelectNodes(".//rest:GeocodePoint/rest:UsageType[.='Display']/parent::node()", nsmgr);
                                string latitude = displayGeocodePoints[0].SelectSingleNode(".//rest:Latitude", nsmgr).InnerText;
                                string longitude = displayGeocodePoints[0].SelectSingleNode(".//rest:Longitude", nsmgr).InnerText;

                                Microsoft.Maps.MapControl.WPF.Location location = new Microsoft.Maps.MapControl.WPF.Location(Convert.ToDouble(latitude), Convert.ToDouble(longitude));
                                Pushpin pushpin = new Pushpin();
                                pushpin.Content = "M";
                                pushpin.Location = location;
                                myMap.Children.Add(pushpin);
                            }
                        }
                    }

                }), DispatcherPriority.Background);
            }

    Tuesday, November 3, 2020 3:38 PM

All replies

  • Hi Mani,

    Have you considered using the UWP control directly? Specifically we have a sample that shows adding POI to a layer then displaying that layer. https://docs.microsoft.com/en-us/windows/uwp/maps-and-location/display-poi

    I would recommend that rather than performing a loop in your main process you look at building a layer and then displaying that layer when it is complete.  The full sample code for UWP is available here Windows-universal-samples/Samples/MapControl at master · microsoft/Windows-universal-samples · GitHub

    Sincerely,

    IoTGirl

    Tuesday, November 3, 2020 4:47 PM
  • Hello 

    The code snippet is from a homegrown application used internally to see outages on a map. I have other functions in the application which gives me the status of each outage which I was trying to display. 

    Currently this works, but I was trying to make it a real time update as the collection of locations with outages are discovered. 

    Can you suggest modifications in my existing code for now, I would consider UWP for the future revamp of the app. 

    Thanks 

    Mani. 

    Tuesday, November 3, 2020 7:13 PM
  • Hi Mani,

    To be clear my recommendation stands as:

    1. Pull this routine outside the main windows process so it doesn't block

    2. Use a layer and update that layer fully and then display rather than updating on each item.

    Sincerely,

    IoTGirl

    Wednesday, November 4, 2020 7:36 PM