C# Windows 10 UWP土工围栏土工围栏必须是圆形吗?
我有一个Windows10UWP应用程序,我正在尝试集成地理围栏。我读过文件说土工栅栏一定是一个圆。这仍然是真的吗?为什么不支持矩形?这看起来很傻,因为我心目中的大多数地理围栏都是长方形的。例如,我的房子,我的院子,建筑物,公园等等。。。它们通常是矩形,而不是圆形 以下是我尝试的代码:C# Windows 10 UWP土工围栏土工围栏必须是圆形吗?,c#,.net,geolocation,windows-10-universal,C#,.net,Geolocation,Windows 10 Universal,我有一个Windows10UWP应用程序,我正在尝试集成地理围栏。我读过文件说土工栅栏一定是一个圆。这仍然是真的吗?为什么不支持矩形?这看起来很傻,因为我心目中的大多数地理围栏都是长方形的。例如,我的房子,我的院子,建筑物,公园等等。。。它们通常是矩形,而不是圆形 以下是我尝试的代码: private Geofence GenerateGeofence() { string fenceKey = new string(Id.Text.ToCharArray());
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通用示例中获取的大部分内容,然后我对其进行了修改。如您所见,API确实有一个GeoboundingBox,它需要一个东北角和一个东南角。似乎有人想到了一个长方形。因此,正如您在代码中看到的,我构建了NW-lat/long和SE-lat/long,并且我能够成功地创建GeoboundingBox对象
但是,在我返回带有此行的新地理围栏后,我得到一个例外:
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);
}
它很好用
那么,有人知道一种方法可以让它与矩形一起工作吗
谢谢 不幸的是,目前唯一受支持的形状是geofence 请检查课程属性的备注 此属性的类型IGeoshape是一个接口,可以支持GeoFence的多种形状。当前唯一受支持的形状是Geocircle,因此在初始化geofences时应该使用这个类
所以虽然有很多,但土工围栏一定是一个圆圈。真可惜。非常感谢。我得想点别的办法。