1

I have some problems with my bing maps.

The first one happens when I click on My Location - from almost all locations I were it worked fine, but there are some locations that returns null, why? (It happened me in a new building that hasn't address yet and also happened in a building with no internet connections).

The method:

private async void MyLocation_Click(object sender, RoutedEventArgs e)
{
    Bing.Maps.Location location = await GeoLocation.GetCurrentLocationAsync();

    MapLayer.SetPosition(_flagPin, location);
    map.SetView(location, 15);
}

The first line calls to my static function:

public static async Task<Bing.Maps.Location> GetCurrentLocationAsync()
{
    Geolocator geo = new Geolocator();
    geo.DesiredAccuracy = PositionAccuracy.Default;

    Geoposition currentPosition = null;
    currentPosition = await geo.GetGeopositionAsync();

    return new Bing.Maps.Location()
    {
        Latitude = currentPosition.Coordinate.Latitude,
        Longitude = currentPosition.Coordinate.Longitude
    };
}

What is the problem? How to fix it?

And the second question is about addresses. When I get an Address object, there are many formats I can select such as FormattedAddress, CountryRegion, PostalTown, I selected The FormattedAddress and there is a problem with it.

My code:

GeocodeResponse GP = await GeoLocation.ReverseGeocodeAsync(location.Latitude, location.Longitude);
EventContext.Address = GP.Results[0].Address.FormattedAddress;

The problem is when I want to send an Address and get the Location. Sometimes this code returns null, why?

GeocodeResponse GP = await GeoLocation.GeocodeAsync(EventContext.Address);

I thought that maybe the problem is that sometimes the Address (Formatted) is not good, sometimes it gives weird addresses, such as, "Street, st. Canada", which is not found and therefore, it returns null. But what can I do to send a correctly Address? Does FomattedAddress is good?

Here are the two GeoCodeAsync and ReverseGeocodeAsync functions:

public static async Task<GeocodeResponse> GeocodeAsync(string address)
{
    GeocodeService.GeocodeRequest geocodeRequest = new GeocodeService.GeocodeRequest();
    // Set credentials using a Bing Maps key
    geocodeRequest.Credentials = new GeocodeService.Credentials();
    geocodeRequest.Credentials.ApplicationId = Application.Current.Resources["BingCredentials"] as string;

    // Set the address
    geocodeRequest.Query = address;

    // Make the geocode request
    GeocodeService.GeocodeServiceClient geocodeService = new GeocodeServiceClient(GeocodeServiceClient.EndpointConfiguration.BasicHttpBinding_IGeocodeService);
    GeocodeResponse geocodeResponse = await geocodeService.GeocodeAsync(geocodeRequest);

    return geocodeResponse;
}

public static async Task<GeocodeResponse> ReverseGeocodeAsync(double latitude, double longitude)
{
    ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();

    // Set credentials using a Bing Maps key
    reverseGeocodeRequest.Credentials = new GeocodeService.Credentials();
    reverseGeocodeRequest.Credentials.ApplicationId = Application.Current.Resources["BingCredentials"] as string;

    // Set the coordinates
    reverseGeocodeRequest.Location = new GeocodeService.GeocodeLocation() { Latitude = latitude, Longitude = longitude };

    // Make the reverse geocode request
    GeocodeServiceClient geocodeService = new GeocodeServiceClient(GeocodeServiceClient.EndpointConfiguration.BasicHttpBinding_IGeocodeService);
    GeocodeResponse geocodeResponse = await geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);

    return geocodeResponse;
}
j0k
  • 22,600
  • 28
  • 79
  • 90
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116

1 Answers1

1

To clarify your first issue, are you getting null from the GPS or is it the address information in the result that is null the issue? If the GPS is returning null then it's possible that your GPS isn't able to get the current position where you are. This has nothing to do with Bing Maps and more so just an issue which your mobile device getting a clear view to the GPS satellites. If the issue is that the address information in the result is null then this is to be expected with new buildings that might not yet be known in the Bing Maps data set. It usually takes several months for new buildings to be found and added to the map data set. If you have no internet connection then the mobile device won't be able to connect to Bing Maps to get the address information. Note that Bing Maps is over 9 Petabytes in size so there is no local copy of the data on your mobile device.

If you already have the coordinates and the address information you shouldn't be geocoding it again. This is a waste of time and will cause issues. The geocoder will sometimes return "street" or "ramp" if the coordinate in which you pass to the reverse geocoder is on an unnamed street. Note geocoders are not designed to clean/validate addresses. They are designed to take an address and return a coordinate. Reverse geocoders are designed to take a coordinate and find the nearest address. Mixing the results from one with the other can result in odd results as the coordinates for each could be significantly different. It rare cases it's possible to loop between both services and get different results each time as the results will slightly be different and end up "walking" along a street.

rbrundritt
  • 16,570
  • 2
  • 21
  • 46