Creating a location service

The first step to allowing our app to take advantage of the device's geolocation capabilities, is to provide an interface in the core library that can be used by the ViewModels in a device- and platform-agnostic manner. When receiving the geolocation back from a device, each platform could potentially provide coordinates in a platform-specific data structure. However, each structure will ultimately provide two double values representing the coordinate's latitude and longitude.

There are a couple of ways to ensure that the results are returned in a platform-agnostic manner, which we will need since we are working in a non-platform-specific library.

One way to ensure this is to pass the values back via a callback method. Another approach we will be employing is to use a custom object, which we will define in our Models namespace, as shown in the following steps:

  1. Create a new class named GeoCoords in the Models folder of the core library.
  2. Add two double properties to the GeoCoords class named Latitude and Longitude:
      public class GeoCoords
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
  1. Create a new interface named ILocationService in the Services folder of the core library. The interface should have one async method, which returns  Task<GeoCoords>:
      public interface ILocationService
{
Task<GeoCoords> GetGeoCoordinatesAsync();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.179.239