• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#利用微软库完成设备网络定位(经纬度-地址)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
    public delegate void OnPositionChangedEventHandle(object sender, PositionChangedEventArgs e);
    public delegate void OnAddressResolveredEventHandle(object sender, AddressResolverEventArgs e);

事件参数类型

    public class AddressResolverEventArgs : PositionChangedEventArgs
    {
        /// <summary>
        /// 地址1
        /// </summary>
        public string Address1 { get; set; }
        /// <summary>
        /// 地址2
        /// </summary>
        public string Address2 { get; set; }
        /// <summary>
        /// 地址3
        /// </summary>
        public string Address3 { get; set; } 
        public AddressResolverEventArgs()
        {

        }
    }
    public class PositionChangedEventArgs : EventArgs
    {
        /// <summary>
        /// 经度
        /// </summary>
        public double Longitude { get; set; }
        /// <summary>
        /// 纬度
        /// </summary>
        public double Latitude { get; set; }

        public object Tag { get; set; }

        public PositionChangedEventArgs()
        {

        }
    }

添加命名空间

using System.Device.Location;
    public class DevicePositioning
    {
        private CivicAddressResolver _address = null;
        private GeoCoordinateWatcher _location = null;
        private GeoCoordinate _lastPosition = GeoCoordinate.Unknown;
        private volatile bool _locationOn = true;
        private bool _resolverByPositionChanged = true;

        public event OnAddressResolveredEventHandle OnAddressResolvered;

        /// <summary>
        /// 当前位置
        /// </summary>
        public GeoCoordinate Position
        {
            get { return _lastPosition; }
        }


        public DevicePositioning()
        {
            _location = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            //
            _location.MovementThreshold = 1.0;//1米
            _location.PositionChanged += Location_PositionChanged;
            //
            _address = new CivicAddressResolver();
            _address.ResolveAddressCompleted += Address_ResolveAddressCompleted;
        }
        /// <summary>
        /// 异步定位取Position值
        /// </summary>
        public void Positioning()
        {
            bool started = false;
            _resolverByPositionChanged = _locationOn = true;
            try
            {
                started = _location.TryStart(true, TimeSpan.FromMilliseconds(1024));
                //_location.TryStart(
                //_location.Start(true);
                //
                if (started)
                {
                    //if (_location.Position.Location.IsUnknown == false)
                    //{
                    //    _address.ResolveAddressAsync(_location.Position.Location);
                    //}
                }
            }
            catch (Exception ex)
            {
                Logs.CallerLog("Error->Positioning:" + ex.Message);
            }
            finally
            {
                if (!started && _locationOn)
                {
                    System.Threading.Thread.Sleep(128);
                    Positioning();
                }
            }
        }
        public void UnPositioning()
        {
            try
            {
                _locationOn = false;
                if (_location != null)
                    _location.Stop();
            }
            catch (Exception ex)
            {
                Logs.CallerLog("Error->UnPositioning:" + ex.Message);
            }
        }

        public void AddressResolver(double lat, double lon)
        {
            InnerAddressResolver(new GeoCoordinate(lat, lon));
        }
        private void InnerAddressResolver(GeoCoordinate position)
        {
            try
            {
                _lastPosition = position;
                _address.ResolveAddressAsync(_lastPosition);
            }
            catch (Exception ex)
            {
                Logs.CallerLog("Error->AddressResolver:" + ex.Message);
            }
        }
        private void Location_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            try
            {
                _lastPosition = e.Position.Location;
                if (!_lastPosition.IsUnknown && _resolverByPositionChanged)
                {
                    _address.ResolveAddressAsync(_lastPosition);
                }
            }
            catch (Exception ex)
            {
                Logs.CallerLog("Error->Location_PositionChanged:" + ex.Message);
            }
            finally
            {
                _resolverByPositionChanged = false;
            }
        }
        private void Address_ResolveAddressCompleted(object sender, ResolveAddressCompletedEventArgs e)
        {
            try
            {
                string address = string.Empty;
                if (e.Address.IsUnknown)
                {
                    address = "Unknown [" + _lastPosition.Longitude + "," + _lastPosition.Latitude + "] Address.";
                }
                else
                {
                    //address = e.Address.AddressLine1;
                    //address = e.Address.AddressLine2;
                    address =
                        e.Address.CountryRegion +
                        e.Address.StateProvince +
                        e.Address.City +
                        e.Address.Building +
                        e.Address.FloorLevel;
                }
                if (OnAddressResolvered != null)
                {
                    OnAddressResolvered.BeginInvoke(this, new AddressResolverEventArgs()
                    {
                        Longitude = _lastPosition.Longitude,
                        Latitude = _lastPosition.Latitude,
                        Address1 = e.Address.AddressLine1,
                        Address2 = e.Address.AddressLine2,
                        Address3 = address
                    }, End_CallBack, null);
                }
            }
            catch (Exception ex)
            {
                Logs.CallerLog("Error->Address_ResolveAddressCompleted:" + ex.Message);
            }
            finally
            {
            }
        }

        private void End_CallBack(IAsyncResult ar)
        {
            try
            {
                if (ar.IsCompleted)
                {
                    if (OnAddressResolvered != null)
                        OnAddressResolvered.EndInvoke(ar);
                }
            }
            catch (Exception ex)
            {
                Logs.CallerLog("Error->End_CallBack:" + ex.Message);
            }
        }
    }

 Demo 用到的程序集  System.Device.dll v4.0 本地电脑上会有的。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#打印、输入和for循环的使用发布时间:2022-07-10
下一篇:
c#坐标系互相转换转自群友的博客:https://www.xiaofengyu.com/?p=108发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap