Example #1
0
        /// <summary>
        /// Handler called when the request for a location's coordinates returns.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReceivedLocationCoordinates(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                return;
            }

            if (e.Error != null)
            {
                if (!Guide.IsVisible)
                {
                    Guide.BeginShowMessageBox("Error focusing on location", e.Error.Message, new string[] { "OK" },
                                              0, MessageBoxIcon.Error, null, null);
                }
                return;
            }

            GeoCoordinate receivedCoordinate;

            try
            {
                // Parse the response XML to get the location's geo-coordinate
                XDocument  locationResponseDoc = XDocument.Load(e.Result);
                XNamespace docNamespace        = locationResponseDoc.Root.GetDefaultNamespace();
                var        locationNodes       = locationResponseDoc.Descendants(XName.Get("Location", docNamespace.NamespaceName));

                if (locationNodes.Count() == 0)
                {
                    Guide.BeginShowMessageBox("Invalid location",
                                              "The requested location was not recognized by the system.", new string[] { "OK" },
                                              0, MessageBoxIcon.Error, null, null);
                    return;
                }

                XElement pointNode = locationNodes.First().Descendants(
                    XName.Get("Point", docNamespace.NamespaceName)).FirstOrDefault();

                if (pointNode == null)
                {
                    Guide.BeginShowMessageBox("Invalid location result", "The location result is missing data.",
                                              new string[] { "OK" }, 0, MessageBoxIcon.Error, null, null);
                    return;
                }

                XElement longitudeNode = pointNode.Element(XName.Get("Longitude", docNamespace.NamespaceName));
                XElement latitudeNode  = pointNode.Element(XName.Get("Latitude", docNamespace.NamespaceName));

                if (longitudeNode == null || latitudeNode == null)
                {
                    Guide.BeginShowMessageBox("Invalid location result", "The location result is missing data.",
                                              new string[] { "OK" }, 0, MessageBoxIcon.Error, null, null);
                    return;
                }

                receivedCoordinate = new GeoCoordinate(double.Parse(latitudeNode.Value),
                                                       double.Parse(longitudeNode.Value));
            }
            catch (Exception err)
            {
                Guide.BeginShowMessageBox("Error getting location coordinates", err.Message, new string[] { "OK" },
                                          0, MessageBoxIcon.Error, null, null);
                return;
            }

            bingMapsViewer.CenterOnLocation(receivedCoordinate);
        }