I am having an issue with Windows Phone 8 Geocoding. Using Microsoft.Phone.Maps.Services.QuerycodeQuery, if I use a full Canadian Postal Code (M5G 1Z4) I get no results. If I use only the first 3 characters (M5G) I get the expected results.
I am not doing anything specific with the data, just passing it to Microsoft for geocoding. Is there a specific reason behind this or is there another way to lookup locations?
Code:
Geocode = new GeocodeQuery()
{
SearchTerm = LocationString,
GeoCoordinate = UserCoordinates ?? new GeoCoordinate(),
MaxResultCount = 1
};
Geocode.QueryCompleted += GetStringLocation_QueryCompleted;
Geocode.QueryCompleted += CleanupGeocode;
Geocode.QueryAsync();
Although this doesn't address the limitation that appears to be in the mapping service's geocode function it is a little workaround that works for me...
When the GetStringLocation_QueryCompleted function returns 0 results I run a regex to check if the search is a valid postal code. If it is one, then I grab the first 3 characters and re-run the geocode.
One thing to make sure of with this approach is that you check to make sure the geocode is not running (IsBusy) when the secondary dispose function is called (if you do it in a separate function)
Regex (Utility.IsPostalCode):
Regex reg = new Regex("^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
return reg.IsMatch(Input);
Within GetStringLocation_QueryCompleted
if (e.Result.Count == 0 && Utility.IsPostalCode(((GeocodeQuery)e.UserState).SearchTerm))
{
if (Geocode != null && Geocode.IsBusy)
Geocode.CancelAsync();
GetStringLocation(((GeocodeQuery)e.UserState).SearchTerm.Substring(0, 3));
}
GetStringLocation would be the function that contains the code that is in the question which accepts a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With