【问题标题】:GPS Location in android in Xamarin using MVVM使用 MVVM 在 Xamarin 中的 android 中的 GPS 定位
【发布时间】:2016-04-12 16:05:34
【问题描述】:

我试图在不使用 Xamarin MVVM 在后台进程的 UI 上显示的情况下获取当前的 GPS 位置,并且在我进行方法调用时无法获取它,我知道事件处理程序会导致问题,有没有解决方法是在我单击它后立即获取它吗?

便携式项目中的代码 -App.cs

public string GetLocation(){
 loc = DependencyService.Get<IMyLocation>();
            loc.locationObtained += (object sender,
                ILocationEventArgs e) => {
                    var lat = e.lat;
                    var lng = e.lng;
                    latitude = lat.ToString();
                    longitude = lng.ToString();
                };
            loc.ObtainMyLocation();
return latitude+":"+longitude;
}

这是我的界面代码:

public interface IMyLocation
{
    void ObtainMyLocation();
    event EventHandler<ILocationEventArgs> locationObtained;
}
public interface ILocationEventArgs
{
    double lat { get; set; }
    double lng { get; set; }

}

最后是 Droid 项目上的依赖服务插件:

[assembly: Xamarin.Forms.Dependency(typeof(GetMyLocation))]
namespace Tutorial.Droid
{
    public class LocationEventArgs : EventArgs, ILocationEventArgs
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }

    public class GetMyLocation : Java.Lang.Object,
                                IMyLocation,
                                ILocationListener
    {
        LocationManager lm;
        public void OnProviderDisabled(string provider) { }
        public void OnProviderEnabled(string provider) { }
        public void OnStatusChanged(string provider,
            Availability status, Android.OS.Bundle extras)
        { }
        //---fired whenever there is a change in location---
        public void OnLocationChanged(Location location)
        {
            if (location != null)
            {
                LocationEventArgs args = new LocationEventArgs();
                args.lat = location.Latitude;
                args.lng = location.Longitude;
                locationObtained(this, args);
            };
        }
        //---an EventHandler delegate that is called when a location
        // is obtained---
        public event EventHandler<ILocationEventArgs>
            locationObtained;
        //---custom event accessor that is invoked when client
        // subscribes to the event---
        event EventHandler<ILocationEventArgs>
            IMyLocation.locationObtained
        {
            add
            {
                locationObtained += value;
            }
            remove
            {
                locationObtained -= value;
            }
        }
        //---method to call to start getting location---
        public void ObtainMyLocation()
        {
            lm = (LocationManager)
                Forms.Context.GetSystemService(
                    Context.LocationService);
            lm.RequestLocationUpdates(
                LocationManager.NetworkProvider,
                    0,   //---time in ms---
                    0,   //---distance in metres---
                    this);
        }
        //---stop the location update when the object is set to
        // null---
        ~GetMyLocation()
        {
            lm.RemoveUpdates(this);
        }
    }
}

【问题讨论】:

    标签: c# android mvvm xamarin gps


    【解决方案1】:

    希望这个项目能解释你的需求https://github.com/raechten/TestGPS...我不是它的作者,只是在网上冲浪时发现的

    【讨论】:

    • 这是我所期待的……非常感谢
    • @Scorpian1990 你有没有用你的代码在 min 解决这个问题,因为它很干净,但是没有触发事件我只得到它一次我使用模拟应用程序更新我的全球定位系统
    猜你喜欢
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    相关资源
    最近更新 更多