zoukankan      html  css  js  c++  java
  • C#利用微软库完成设备网络定位(经纬度-地址)

        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 本地电脑上会有的。

  • 相关阅读:
    hdu 2444 交叉染色判断二分图+二分最大匹配
    uva 交叉染色法10004
    poj 3177&&3352 求边双联通分量,先求桥,然后求分量( 临界表代码)
    poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的
    poj 3006水题打素数表
    POJ 3352 无向图边双连通分量,缩点,无重边
    hdu 1430 魔板 康托展开 + 很好的映射
    D. Artsem and Saunders 数学题
    vijos P1412多人背包 DP的前k优解
    1475 建设国家 DP
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/5805887.html
Copyright © 2011-2022 走看看