我遇到了Windows Phone 8 Geocoding的问题。 使用Microsoft.Phone.Maps.Services.QuerycodeQuery ,如果我使用完整的加拿大邮政编码(M5G 1Z4),我没有得到任何结果。 如果我只使用前3个字符(M5G),我会得到预期的结果。
我没有做任何具体的数据,只是将它传递给微软进行地理编码。 这背后是否存在具体原因,还是有其他方法可以查找位置?
码:
Geocode = new GeocodeQuery() { SearchTerm = LocationString, GeoCoordinate = UserCoordinates ?? new GeoCoordinate(), MaxResultCount = 1 }; Geocode.QueryCompleted += GetStringLocation_QueryCompleted; Geocode.QueryCompleted += CleanupGeocode; Geocode.QueryAsync();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();最满意答案
虽然这没有解决地图服务的地理编码功能中出现的限制,但这是一个适合我的小解决方法......
当GetStringLocation_QueryCompleted函数返回0结果时,我运行一个正则表达式来检查搜索是否是有效的邮政编码。 如果它是一个,那么我抓住前3个字符并重新运行地理编码。
确保使用这种方法的一件事是,在调用辅助dispose函数时检查以确保地理编码未运行(IsBusy)(如果在单独的函数中执行)
正则表达式(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);在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将是包含接受字符串的问题中的代码的函数。
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.
更多推荐
发布评论