0

I am developing an application in Windows Phone Silverlight 8.1 in which I am trying to set a route between current location and a hardcoded destination using this link the code is:

private async void GetCoordinates()
        {
            Geolocator myGeolocator = new Geolocator();
            myGeolocator.DesiredAccuracyInMeters = 50;
            Geoposition myposition = null;
            try
            {
                myposition = await myGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1),TimeSpan.FromSeconds(50));
                Geocoordinate myGeocoordinate = myposition.Coordinate;
                GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
                mycoordinates.Add(new GeoCoordinate(myGeoCoordinate.Latitude, myGeoCoordinate.Longitude));
                locationmap.Center = myGeoCoordinate;
                locationmap.ZoomLevel = 13;

                Mygeocodequery = new GeocodeQuery();
                Mygeocodequery.SearchTerm = "Seattle, WA";
                Mygeocodequery.GeoCoordinate = new GeoCoordinate(myGeoCoordinate.Latitude, myGeoCoordinate.Longitude);
                Mygeocodequery.QueryCompleted += Mygeocodequery_Querycompleted;
                Mygeocodequery.QueryAsync();
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Location is disabled in phone settings or capabilities are not checked.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Mygeocodequery_Querycompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
        {
            if(e.Error==null)
            {
                MyQuery = new RouteQuery();
                mycoordinates.Add(e.Result[0].GeoCoordinate);
                MyQuery.Waypoints = mycoordinates;
                MyQuery.QueryCompleted += MyQuery_QueryCompleted;
                MyQuery.QueryAsync();
                Mygeocodequery.Dispose();
            }
        }

        private void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
        {
            try
            {
                    **Route MyRoute = e.Result;**
                    MapRoute MyMapRoute = new MapRoute(MyRoute);
                    locationmap.AddRoute(MyMapRoute);
                    MyQuery.Dispose();
            }
            catch (TargetInvocationException ex)
            {
                MessageBox.Show(ex.InnerException.Message);
            }
        }

It sometimes works fine but sometimes I am getting this inner exception as TargetInvocationException Exception from HRESULT: 0x8004231C

On the line highlighted please let me know what should I do?

Vinita
  • 61
  • 8

1 Answers1

0

The Problem is only because I had skipped the step to write the following code in load event of map control

private void locationmap_Loaded(object sender, RoutedEventArgs e)
    {
        Microsoft.Phone.Maps.MapsSettings.ApplicationContext.ApplicationId = "yourapplicationID";
        Microsoft.Phone.Maps.MapsSettings.ApplicationContext.AuthenticationToken = "yourauthenticationtoken";
    }

if this code is added it works fine for all the locations.

Vinita
  • 61
  • 8