public void LocationForAddress(string addressString, GeoCoordinate searchNearLocation, object callerState) { GeocodeRequest request = new GeocodeRequest(); request.Credentials = new dev.virtualearth.net.webservices.v1.common.Credentials() { ApplicationId = bingMapsApiKey }; request.Query = addressString; request.UserProfile = new UserProfile() { CurrentLocation = new UserLocation() { Latitude = searchNearLocation.Latitude, Longitude = searchNearLocation.Longitude }, DistanceUnit = DistanceUnit.Mile, DeviceType = DeviceType.Mobile, ScreenSize = new SizeOfint() { Width = 480, Height = 700 } }; GeocodeState state = new GeocodeState() { Query = addressString, SearchNearLocation = searchNearLocation, CallerState = callerState }; geocodeService.GeocodeAsync(request, state); }
void geocodeService_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e) { Exception error = e.Error; List <LocationForQuery> locations = new List <LocationForQuery>(); if (error == null) { try { GeocodeResult[] results = e.Result.Results; foreach (GeocodeResult result in results) { LocationForQuery location = new LocationForQuery() { name = result.DisplayName, boundingBox = new LocationRect() { Northeast = new GeoCoordinate(result.BestView.Northeast.Latitude, result.BestView.Northeast.Longitude), Southwest = new GeoCoordinate(result.BestView.Southwest.Latitude, result.BestView.Southwest.Longitude) }, confidence = (ViewModel.LocationServiceDataStructures.Confidence)(int) result.Confidence }; location.location = new GeoCoordinate() { Latitude = result.Locations[0].Latitude, Longitude = result.Locations[0].Longitude, Altitude = result.Locations[0].Altitude }; locations.Add(location); } } catch (Exception ex) { error = ex; } } Debug.Assert(error == null); if (LocationForAddress_Completed != null) { GeocodeState state = (GeocodeState)e.UserState; LocationForAddress_Completed(this, new LocationForAddressEventArgs( locations, state.Query, state.SearchNearLocation, error, state.CallerState )); } }