Calvin Nunes
Calvin Nunes

Reputation: 6516

In Xamarin How to get device location to use in Google Maps routes

I'm trying to use some kind of deep link to from my app, open google maps with a route to follow. I'm using Xamarin portable, and I'm using this code, that is working, but is keeping the starting postion of the route, blank.

var latDestiny = ClientMap.Latitude.ToString().Replace(",", ".");
var longiDestiny = ClientMap.Longitude.ToString().Replace(",", ".");

string destiny = "&destination=" + latDestino + "," + longiDestino;

var gMapsURL = "https://www.google.com/maps/dir/?api=1" + destiny;
GlobalLib.OpenExternalURL(gMapsURL);

I saw that i need to add something like this to the link:

"&origin=" + myLatitude + "," + myLongitude;

but all the ways I find to get the device postion are not clear and looks very complicated, does someone have a good solution to find the device latitude and longigute?

Upvotes: 1

Views: 1362

Answers (1)

Calvin Nunes
Calvin Nunes

Reputation: 6516

Try this idea: (I use in my app and it works nice)

Create an Interface in the portable project: (yes, you will need to treat the current location differently in each platform)

namespace YourProject
{
    public interface ILibNative
    {                
        Position ReturnDevicePosition();
    }
}

then, create a class that derives the interface inside iOS and Android projects: ANDROID:

[assembly: Dependency(typeof(LibNative_DROID))]
namespace YourProject.Droid
{
    public class LibNative_DROID : ILibNative
    {

        public LibNative_DROID()
        {
        }

        public Position ReturnDevicePosition()
        {
            Position myPosition = new Position();
            try
            {
                Accuracy DesiredAccuracy = Accuracy.Fine;
                var locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
                if (locationManager.AllProviders.Count > 0)
                {
                    var criteria = new Criteria
                    {
                        Accuracy = DesiredAccuracy
                    };

                    var provider = locationManager.GetBestProvider(criteria, true);

                    myPosition = new Position(locationManager.GetLastKnownLocation(provider).Latitude, locationManager.GetLastKnownLocation(provider).Longitude);
                }
                return myPosition;
            }
            catch
            {
                return myPosition;
            }
        }
    }
}

iOS:

[assembly: Dependency(typeof(LibNative_iOS))]
namespace YourProject.iOS
{
    public class LibNative_iOS : ILibNative
    {

        public LibNative_iOS()
        {
        }              

        public Position ReturnDevicePosition()
        {
            Position myPosition = new Position();
            try
            {
                if (CLLocationManager.LocationServicesEnabled)
                {
                    CLLocationManager locationManager = new CLLocationManager();
                    locationManager.RequestWhenInUseAuthorization();
                    var location = locationManager.Location;
                    myPosition = new Position(location.Coordinate.Latitude, location.Coordinate.Longitude);
                }
                return myPosition;
            }
            catch
            {
                return myPosition;
            }
        }
    }
}

Don't forget to ask for enabled access to your location in the manifests (fine_location, coarse_location, gps, internet, etc...)

then, after did that, you can do this:

var MyPosition = new Position();
MyPosition = DependencyService.Get<ILibNative>().ReturnDevicePosition(); 
var myLatitude = MyPosition.Latitude.ToString().Replace(",", ".");
var myLongitude = MyPosition.Longitude.ToString().Replace(",", ".");
string startingPoint = "&origin=" + myLatitude + "," + myLongitude;

Now, just add 'startingPoint' to your googleMaps URL...

Upvotes: 1

Related Questions