首页 文章

在Xamarin中如何获取设备位置以在Google Maps路线中使用

提问于
浏览
2

我正在尝试使用某种深层链接从我的应用程序,打开谷歌 Map 与路线 . 我正在使用Xamarin便携式,我正在使用这个代码,这是有效的,但保持路由的起始位置,空白 .

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);

我看到我需要在链接中添加这样的内容:

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

但是我发现获得设备位置的所有方法都不清楚,看起来很复杂,有人有一个很好的解决方案来找到设备的纬度和长度吗?

1 回答

  • 1

    尝试这个想法:(我在我的应用程序中使用,它很好用)

    在便携式项目中创建 Interface :(是的,您需要在每个平台中以不同方式处理当前位置)

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

    然后,创建一个派生iOS和Android项目内部接口的类: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...)

    然后,完成后,你可以这样做:

    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;
    

    现在,只需将“startingPoint”添加到googleMaps网址即可...

相关问题