Replicating Microsoft News with Xamarin Part 11 (Using Geo Location)

Geo Location… Mmmh… 🙂… If this isn’t arguably the single most powerful feature of smartphones after cellular capabilities.

Well, will learn how to tap into this powerful feature on this one.

Let’s get down to it.

The use case for this is, we want a user to receive Local News depending on their location. So what are we going to do? Firstly get the current location of the user, then search for News using the user’s location as the key word. For this we will just use the user’s country, but if you prefer, you can fine tune the location details.

In your ‘ViewModels’ folder create an empty class and name it ‘LocalViewModel’ as shown below:

Xamarin.Essentials has Geo Location capabilities, lucky enough the latest Xamarin ships with Xamarin.Essentials therefore we won’t have to install a 3rd party Library.

First things first. Permissions, permissions permissions!! We need to handle permission request to the user.

Android:

On Android, we first need some prerequisites.

First thing we need to do is head over to AssemblyInfo in Properties as shown below:

In AssemblyInfo add the following piece of code:

Secondly, let’s add permissions and usings to the AndroidManifest:

On iOS:

Let’s head over to the iOS info.plist file as shown below:

We’ll need to add the following to the info.plist file:

That’s all for iOS.

We are now going to write a Task that gets the user’s current location and returns the user’s country as a string.

Xamarin.Essentials has done a lot of the heavy lifting for us and all we’ll need is the piece of code below:

The full ‘LocalViewModel’ code is as below:

No all we need to do is to wire up the View… So let’s head over to the ‘Local’ page. Full code below:

xaml:

.cs:

Great!! 😎

Let’s run the code…

In our next blog post we will create a Firebase Project and register our application 🙂.

Link to branch: https://github.com/Elisha-Misoi/Nomadic/tree/feature/geo-location

See you in Part 12 (Using Creating a Firebase Project) 😉

All relevant links: https://link.medium.com/fsVGOOLP15

Disclaimer ☠!!

As much as consideration was taken for iOS platform-specific code, this application has not been tested on iOS. In theory it should work, but whatever bugs that you may encounter or on the event that the application does not successfully build or for whatever reason your device bursts into flames, I might not be of help. Sorry 😐.

A certain amount of code might end up missing in the blog post please refer to the branch link.

I might not be able to explain every single line of code so please refer to comments in the code for more explanation.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store