Windows 10 UWP Geofencing地理围栏必须是一个圆圈?

时间:2017-05-09 23:59:14

标签: c# .net geolocation windows-10-universal

我有一个Windows 10 UWP应用程序,我正在尝试集成Geofencing。我已经阅读了Geofence必须是一个圆圈的文件。这是真的吗?为什么不支持矩形?这看起来很愚蠢,因为在我看来,大多数地理围栏都是矩形的。例如,我的房子,我的院子,建筑物,公园等......比它们更像是矩形(通常)。

这是我尝试过的代码:

    private Geofence GenerateGeofence()
    {
        string fenceKey = new string(Id.Text.ToCharArray());

        BasicGeoposition positionNW;
        positionNW.Latitude = double.Parse(LatitudeNW.Text);
        positionNW.Longitude = double.Parse(LongitudeNW.Text);
        positionNW.Altitude = 0.0;

        BasicGeoposition positionSE;
        positionSE.Latitude = double.Parse(LatitudeSE.Text);
        positionSE.Longitude = double.Parse(LongitudeSE.Text);
        positionSE.Altitude = 0.0;

        // the geofence can be a circular region. However, we are going to use a rectangle
        GeoboundingBox geoRect = new GeoboundingBox(positionNW, positionSE);

        //Lock into false for single use because we don't want that feature for now.
        bool singleUse = false;

        // want to listen for enter geofence, exit geofence and remove geofence events
        // you can select a subset of these event states
        MonitoredGeofenceStates mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited | MonitoredGeofenceStates.Removed;

        TimeSpan dwellTime;
        TimeSpan duration;
        DateTimeOffset startTime;
        var maxTimeSpan = TimeSpan.MaxValue;

        try
        {
            //We are going to just hard set the dwell time to 5 seconds for now.
            dwellTime = new TimeSpan(ParseTimeSpan("0", defaultDwellTimeSeconds));
            // setting up how long you need to be in geofence for enter event to fire
            //if (string.Empty != DwellTime.Text)
            //{
            //    dwellTime = new TimeSpan(ParseTimeSpan(DwellTime.Text, defaultDwellTimeSeconds));
            //}
            //else
            //{
            //    dwellTime = new TimeSpan(ParseTimeSpan("0", defaultDwellTimeSeconds));
            //}

            // setting up how long the geofence should be active
            if (string.Empty != Duration.Text)
            {
                duration = new TimeSpan(ParseTimeSpan(Duration.Text, 0));
            }
            else
            {
                duration = maxTimeSpan;
            }

            // setting up the start time of the geofence
            if (string.Empty != StartTime.Text)
            {
                startTime = DateTimeOffset.Parse(StartTime.Text);
            }
            else
            {
                // if you don't set start time in C# the start time defaults to 1/1/1601
                calendar.SetToNow();
                startTime = calendar.GetDateTime();
            }
        }
        catch (ArgumentNullException)
        {
        }
        catch (FormatException)
        {
            _rootPage.NotifyUser("Entered value is not a valid string representation of a date and time", NotifyType.ErrorMessage);
        }
        catch (ArgumentException)
        {
            _rootPage.NotifyUser("The offset is greater than 14 hours or less than -14 hours.", NotifyType.ErrorMessage);
        }

        return new Geofence(fenceKey, geoRect, mask, singleUse, dwellTime, startTime, duration);
    }

这大部分来自Windows Universal样本,然后我对其进行了修改。正如您所看到的,API确实有一个GeoboundingBox,它需要一个东北角和一个东南角。似乎有一个矩形被认为。所以,正如你在代码中看到的,我构建了NW lat / long和SE lat / long,我能够成功创建一个GeoboundingBox对象。

然而,在我用这一行返回新的Geofence后,我得到一个例外:

return new Geofence(fenceKey, geoRect, mask, singleUse, dwellTime, startTime, duration);

Geofence对象构造函数只是说它需要传入一个形状,但它显然不喜欢边界框。如果我将代码更改回一个圆圈,如下所示:

    private Geofence GenerateGeofence()
    {
        string fenceKey = new string(Id.Text.ToCharArray());

        BasicGeoposition positionNW;
        positionNW.Latitude = double.Parse(LatitudeNW.Text);
        positionNW.Longitude = double.Parse(LongitudeNW.Text);
        positionNW.Altitude = 0.0;

        BasicGeoposition positionSE;
        positionSE.Latitude = double.Parse(LatitudeSE.Text);
        positionSE.Longitude = double.Parse(LongitudeSE.Text);
        positionSE.Altitude = 0.0;

        // the geofence can be a circular region. However, we are going to use a rectangle
        Geocircle geoCircle = new Geocircle(positionNW, 5.0);

        //Lock into false for single use because we don't want that feature for now.
        bool singleUse = false;

        // want to listen for enter geofence, exit geofence and remove geofence events
        // you can select a subset of these event states
        MonitoredGeofenceStates mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited | MonitoredGeofenceStates.Removed;

        TimeSpan dwellTime;
        TimeSpan duration;
        DateTimeOffset startTime;
        var maxTimeSpan = TimeSpan.MaxValue;

        try
        {
            //We are going to just hard set the dwell time to 5 seconds for now.
            dwellTime = new TimeSpan(ParseTimeSpan("0", defaultDwellTimeSeconds));
            // setting up how long you need to be in geofence for enter event to fire
            //if (string.Empty != DwellTime.Text)
            //{
            //    dwellTime = new TimeSpan(ParseTimeSpan(DwellTime.Text, defaultDwellTimeSeconds));
            //}
            //else
            //{
            //    dwellTime = new TimeSpan(ParseTimeSpan("0", defaultDwellTimeSeconds));
            //}

            // setting up how long the geofence should be active
            if (string.Empty != Duration.Text)
            {
                duration = new TimeSpan(ParseTimeSpan(Duration.Text, 0));
            }
            else
            {
                duration = maxTimeSpan;
            }

            // setting up the start time of the geofence
            if (string.Empty != StartTime.Text)
            {
                startTime = DateTimeOffset.Parse(StartTime.Text);
            }
            else
            {
                // if you don't set start time in C# the start time defaults to 1/1/1601
                calendar.SetToNow();
                startTime = calendar.GetDateTime();
            }
        }
        catch (ArgumentNullException)
        {
        }
        catch (FormatException)
        {
            _rootPage.NotifyUser("Entered value is not a valid string representation of a date and time", NotifyType.ErrorMessage);
        }
        catch (ArgumentException)
        {
            _rootPage.NotifyUser("The offset is greater than 14 hours or less than -14 hours.", NotifyType.ErrorMessage);
        }

        return new Geofence(fenceKey, geoCircle, mask, singleUse, dwellTime, startTime, duration);
    }

工作正常。

那么,是否有人知道我可以用矩形工作的方法?

谢谢!

1 个答案:

答案 0 :(得分:1)

不幸的是,目前支持的唯一形状是地理位置Geocircle

请查看Geoshape课程Geofence属性的评论。

  

此属性的类型IGeoshape是一个接口,可以支持地理围栏的多种形状。目前支持的唯一形状是Geocircle,因此这是初始化地理围栏时应该使用的类。

所以尽管有Geoshape​Types,但地理围栏必须是一个圆圈。