I modified a bit Benoit's answer to look like this:
public static Task<string> StartReverseGeoCodingAsync( System.Device.Location.GeoCoordinate Location )
{
var reverseGeocode = new ReverseGeocodeQuery();
reverseGeocode.GeoCoordinate = new System.Device.Location.GeoCoordinate( Location.Latitude, Location.Longitude );
var tcs = new TaskCompletionSource<string>();
EventHandler<QueryCompletedEventArgs<System.Collections.Generic.IList<MapLocation>>> handler = null;
handler = ( sender, args ) =>
{
if ( args.Error != null )
{
tcs.SetException( args.Error );
}
else if ( args.Cancelled )
{
tcs.SetCanceled();
}
else
{
Addresses.Clear();
foreach ( var address in args.Result.Select( adrInfo => adrInfo.Information.Address ) )
{
Addresses.Add(
string.Format( "{0} {1}, {2} {3} {4}, {5}",
address.HouseNumber,
address.Street,
address.City,
address.State,
address.PostalCode,
address.Country ).Trim() );
}
string Address = Addresses.Count > 0 ? Address = Addresses[ 0 ].ToString() : string.Empty;
reverseGeocode.QueryCompleted -= handler;
tcs.SetResult( Address );
}
};
reverseGeocode.QueryCompleted += handler;
reverseGeocode.QueryAsync();
return tcs.Task;
}
this will replace the following two functions in my code:
#if never
public static void StartReverseGeoCoding( GeoCoordinate Location )
{
var reverseGeocode = new ReverseGeocodeQuery();
reverseGeocode.GeoCoordinate = new GeoCoordinate( Location.Latitude, Location.Longitude );
reverseGeocode.QueryCompleted += ReverseGeocodeQueryCompleted;
reverseGeocode.QueryAsync();
}
public static void ReverseGeocodeQueryCompleted( object sender, QueryCompletedEventArgs<System.Collections.Generic.IList<MapLocation>> e )
{
var reverseGeocode = sender as ReverseGeocodeQuery;
if ( reverseGeocode != null )
{
reverseGeocode.QueryCompleted -= ReverseGeocodeQueryCompleted;
}
// Microsoft.Phone.Maps.Services.MapAddress address;
Addresses.Clear();
if ( !e.Cancelled )
{
foreach ( var address in e.Result.Select( adrInfo => adrInfo.Information.Address ) )
{
Addresses.Add( string.Format( "{0} {1}, {2} {3} {4}, {5}",
address.HouseNumber,
address.Street,
address.City,
address.State,
address.PostalCode,
address.Country ).Trim() );
}
}
Address = ( Addresses.Count > 0 ) ? Addresses[ 0 ].ToString() : string.Empty;
}
endif
Overall works fine, and again thanks to Benoit!