Hi!
If you ever get bored, an 100% interesting activity is to try to update a project that has been closed 3 years ago or more. In my case, I have invested much time during these past days upgrading a Windows 8.1 to Windows 10 App.
Here’s a couple of things I’ve learned
- Visual Studio 2017 is a thousand times better than previous Visual Studio versions to create UWP Apps. This point applies the saying “for tastes colors” in my case Visual Studio 2017 works great!
- 3 years ago the most advisable option to share code/functionality between projects were to use Portable Class Libraries. Today this is very Old School, so I have migrated several projects to .Net Standard and it was not an easy path (ostias!)
- I have suffered, in silence but pain was there. For example, .Net Standard its now in version 2.0, however, in order to create Net standard Class Libraries that can be used in UWP Apps, you have to “download” to version 1.4
- No more CivicAddress. If you use localization functionality in your App, including values such as the country, city, zip code and State, the CivicAddress property has been declared obsolete from win 8.1
On this last point I will extend a bit. Well When you get the location in a UWP App, the 2 values that if returned are latitude and longitude. I was investigating a little bit about the best way to get an address from a point (latitude and longitude) and I came to this conclusion.
You can use services like the following where you must have a Developer key
Although thanks to StackOverflow I found a free service that with 4 lines solves the problem. This is OPENSTREETMAP and the following code is more than enough to understand how it works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<Tuple<string, string, string, string>> ResolveCountryAndCityNameAsync(double latitude, double longitude, string appName) | |
{ | |
var lat = latitude.ToString(CultureInfo.InvariantCulture).Replace(",", "."); | |
var lon = longitude.ToString(CultureInfo.InvariantCulture).Replace(",", "."); | |
var requestUri = $"http://nominatim.openstreetmap.org/reverse?format=xml&zoom=18&lat={lat}&lon={lon}&application={appName}"; | |
var client = new HttpClient(); | |
var resTask = client.GetStringAsync(requestUri); | |
var res = await resTask; | |
var resultDocument = XDocument.Parse(res); | |
var addressElement = resultDocument.Root.Element("addressparts"); | |
var city = addressElement.Element("city").Value; | |
var country = addressElement.Element("country").Value; | |
var postalCode = addressElement.Element("postcode").Value; | |
var state = addressElement.Element("state").Value; | |
var result = new Tuple<string, string, string, string>(country, city, postalCode, state); | |
return result; | |
} |
A sample result in the next image
that if, if your application requires a very intense use of this service, it is time to go back to Bing or Google to avoid a
System.Net.Http.HttpRequestException: ‘Response status code does not indicate success: 429 (Too Many Requests).’
Happy Coding!
Greetings @ Toronto
El Bruno
References
- Blogs MSDN. Converting PCL (Portable Class Libraries) to .NET Standard Class Libraries
- GeoLocation CivicAddress
- OpenStreetMap
- HTTP Response 429, Too many requests