设置地理围栏 (HTML)
[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]
本主题将指导你在应用中逐步设置 Geofence。
路线图: 本主题与其他主题有何关联?请参阅:
简介
在设置Geofence时,需要执行较多的步骤。 除了定义关注的区域之外,你还需要确保拥有正确的位置权限。 最后,你需要设置一个事件处理程序,以防正在运行应用时用户更改那些权限。
验证是否启用了位置
在你的应用可以访问位置之前,必须在设备上启用“位置”。在“设置”****应用中,检查以下“位置隐私设置”是否已打开:
- “此设备的位置...”****已“打开”(在 Windows 10 移动版中不适用)
- 位置服务设置(“位置”****)已“打开”
- 在“选择可以使用你的位置的应用”****下,你的应用已设置为“打开”
启用位置功能
在“解决方案资源管理器”****中,双击“package.appxmanifest”并选择“功能”选项卡。然后选中“功能”列表中的“位置”。这将向程序包清单文件中添加 Location
设备功能。
<Capabilities>
<!-- DeviceCapability elements must follow Capability elements (if present) -->
<DeviceCapability Name="location"/>
</Capabilities>
检查位置权限
首先可能需要在初始化期间向应用添加代码,以此来获得位置。 在 Windows 上,你的应用首次使用 API 获取当前位置时,系统将提示用户授予位置权限。 如果应用没有来自用户的权限,则会提醒用户。 注意,你仍可以设置一个没有位置权限的Geofence,但是只有在启用了权限以后,你才能收到通知。
function initialize() {
promise = geolocator.getGeopositionAsync();
promise.done(
function (pos) {
var coord = pos.coordinate;
},
function (err) {
// handle situations where location permissions are not granted to your app
}
);
}
侦听位置权限有无更改
接下来,你需要确保注册权限更改事件,以防用户出于某种原因决定禁用位置权限。 首先将事件处理程序添加到初始化方法中:
var accessInfo = null;
accessInfo = DeviceAccessInformation.createFromDeviceClass(Enumeration.DeviceClass.location);
accessInfo.addEventListener("accesschanged", onAccessChanged);
然后处理权限更改,以便在位置权限被禁用时,允许用户知晓地理围栏功能将不再运行。
function onAccessChanged(args) {
var eventDescription = getTimeStampedMessage("Device Access Status");
var item = null;
if (DeviceAccessStatus.deniedByUser === args.status) {
eventDescription += " (DeniedByUser)";
WinJS.log && WinJS.log("Location has been disabled by the user. Enable access through the settings charm.", "sample", "status");
} else if (DeviceAccessStatus.deniedBySystem === args.status) {
eventDescription += " (DeniedBySystem)";
WinJS.log && WinJS.log("Location has been disabled by the system. The administrator of the device must enable location access through the location control panel.", "sample", "status");
} else if (DeviceAccessStatus.unspecified === args.status) {
eventDescription += " (Unspecified)";
WinJS.log && WinJS.log("Location has been disabled by unspecified source. The administrator of the device may need to enable location access through the location control panel, then enable access through the settings charm.", "sample", "status");
} else if (DeviceAccessStatus.allowed === args.status) {
eventDescription += " (Allowed)";
// clear status
WinJS.log && WinJS.log("", "sample", "status");
} else {
eventDescription += " (Unknown)";
WinJS.log && WinJS.log("Unknown device access information status", "sample", "status");
}
addEventDescription(eventDescription);
}
注意 你可以通过检查 LocationStatus 属性确定用户是否在“设置”中禁用了位置。如果值为 Disabled,则禁用位置。
创建地理围栏
现在可以准备定义和设置地理围栏了。 可以设置以下地理围栏值:
- 用于识别地理围栏的 Id。
- 圆形关注区域,由Geoshape定义。
- MonitoredStates,指明需要为哪些地理围栏事件接收通知 - 输入定义的区域、保留定义的区域或者删除地理围栏。
- SingleUse标志,当满足了监视地理围栏的所有状态时,将删除地理围栏。
- DwellTime,指明在触发进入/退出事件之前,用户必须位于所定义区域之内/之外的时间。
- StartTime,指明何时开始监视地理围栏。
- 监视地理围栏的Duration。
function generateGeofence() {
var geofence = null;
try {
var fenceKey = nameElement.value;
var position = {
latitude: decimalFormatter.parseDouble(latitude.value),
longitude: decimalFormatter.parseDouble(longitude.value),
altitude: 0
};
var radiusValue = decimalFormatter.parseDouble(radius.value);
// the geofence is a circular region
var geocircle = new Windows.Devices.Geolocation.Geocircle(position, radiusValue);
var singleUse = false;
if (geofenceSingleUse.checked) {
singleUse = true;
}
// want to listen for enter geofence, exit geofence and remove geofence events
var mask = 0;
mask = mask | Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.entered;
mask = mask | Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.exited;
mask = mask | Windows.Devices.Geolocation.Geofencing.MonitoredGeofenceStates.removed;
var dwellTimeSpan = new Number(parseTimeSpan(dwellTimeField, defaultDwellTimeSeconds));
var durationTimeSpan = null;
if (durationField.value.length) {
durationTimeSpan = new Number(parseTimeSpan(durationField, 0));
} else {
durationTimeSpan = new Number(0); // duration needs to be set since start time is set below
}
var startDateTime = null;
if (startTimeField.value.length) {
startDateTime = new Date(startTimeField.value);
} else {
startDateTime = new Date(); // if you don't set start time in JavaScript the start time defaults to 1/1/1601
}
geofence = new Windows.Devices.Geolocation.Geofencing.Geofence(fenceKey, geocircle, mask, singleUse, dwellTimeSpan, startDateTime, durationTimeSpan);
} catch (ex) {
WinJS.log && WinJS.log(ex.toString(), "sample", "error");
}
return geofence;
}
相关主题
路线图
任务
参考
其他资源