none
How to remove a pushpin? RRS feed

  • Question

  • Hi,

    I have a Bing Map app that I am trying to finsh and I am stuck on a bit of code I can not seem to figure out.

    I have a Bing Map that has 2 pushpins on it (pin1 & pin2).  On the PositionChanged()  I reuse the same location for pin1 but pin2 needs to be deleted (map1.Children.Remoce(pin2)) the with new lat & Lon positions add the pin again.

    I just cann't seem to figure out where to remove the pin2 line?

    POSITION CHANGED....
    void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
            {
                try
                {
                    ClearDriftHistory();
                    // get the stored Anchor position
                    CLat = Convert.ToString(e.Position.Location.Latitude);
                    CLon = Convert.ToString(e.Position.Location.Longitude);
                    // populate the form
                    // go get the distance between points
                    double d = llc.OnDistanceChanged(Convert.ToDouble(ALat), Convert.ToDouble(ALon), Convert.ToDouble(CLat), Convert.ToDouble(CLon));
                    Drift.Text = "Drift from Anchor position... " + d.ToString("0.00") + " meters";
                    mapPins();
                }
                catch (Exception ex)
                {
                    // do something here with the error
                    MessageBox.Show("Conversion error - " + ex, "AnchorDrift ERROR", MessageBoxButton.OK);
                }
            }

    //----------------------------------------------------------------------

    MAPPINS()........
            private void mapPins()
            {
                try {
                    Pushpin pin1 = new Pushpin();
                    Pushpin pin2 = new Pushpin();

                    //--------------------------------------------------------------------------
                    // ORIGINAL WORKING CODE - 1 pin
                    // map1 is created and defined on the XML (form)
                    //--------------------------------------------------------------------------
                    // Set the center (Anchor) coordinate
                    GeoCoordinate mapCenter = new GeoCoordinate(Convert.ToDouble(ALat), Convert.ToDouble(ALon));
                    // set the current/drift position
                    GeoCoordinate driftPistion = new GeoCoordinate(Convert.ToDouble(CLat), Convert.ToDouble(CLon));
                    // ------------------------------------------------------------
                    // conver to standard Nautical DMS format
                    string modeALat = "";
                    string modeALon = "";
                    string modeCLat = "";
                    string modeCLon = "";
                    if (newAS.getLocalStorageItem("ChartMode") == "Nautical")
                    {
                         modeALat = llc.decToDeg(Convert.ToDouble(ALat));
                         modeALon = llc.decToDeg(Convert.ToDouble(ALon));
                         modeCLat = llc.decToDeg(Convert.ToDouble(CLat));
                         modeCLon = llc.decToDeg(Convert.ToDouble(CLon));
                    }
                    else
                    {
                        modeALat = ALat;
                        modeALon = ALon;
                        modeCLat = CLat;
                        modeCLon = CLon;

                    }
                    // ------------------------------------------------------------
                    // Create a pushpin to put at the center of the view
                    pin1.Location = mapCenter;
                    pin1.Content = "Anchor Position - \n" + modeALat + "x" + modeALon;
                    map1.Children.Add(pin1);

                    // create a pushpin to put the drift position
                    pin2.Location = driftPistion;
                    pin2.Content = "Drift Position - \n" + modeCLat + "x" + modeCLon;
                    map1.Children.Add(pin2);
                    // -------------------------------------------------------------
                    // Set the map style to Aerial
                    map1.Mode = new Microsoft.Phone.Controls.Maps.AerialMode();
                    // Set the view and put the map on the page
                    map1.SetView(LocationRect.CreateLocationRect(pin2.Location, pin1.Location));
                }
                catch (Exception)
                {
                    MessageBox.Show("Mapping failed.", "ERROR", MessageBoxButton.OK);
                }
            }
            // -----------------------------------------------------------------------------------

    Anyone got any suggestions?  I know that I can not "refresh" the location value for a pushpin, that it has to be removed and a new pin added.

    Thanks


    SquireDude

    Tuesday, May 8, 2012 5:31 PM

Answers

  • Okay I found a good place to remove the statement....

            void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
            {
                try
                {
                    ClearDriftHistory();
                    map1.Children.Remove(pin2);
                    // get the stored Anchor position
                    CLat = Convert.ToString(e.Position.Location.Latitude);
                    CLon = Convert.ToString(e.Position.Location.Longitude);


    SquireDude

    • Marked as answer by SquireDude Wednesday, May 9, 2012 12:04 AM
    Wednesday, May 9, 2012 12:04 AM