maui-geolocation

在 .NET MAUI 应用中使用 Microsoft.Maui.Devices.Sensors 添加地理定位功能,涵盖单次定位与持续定位、各平台(Android、iOS、macOS、Windows)权限、精度级别、CancellationToken 用法、模拟位置检测与支持依赖注入的服务封装。适用于获取当前位置、持续定位与位置跟踪等场景;不包括在地图上显示位置、通用权限处理或地址地理编码。

仓库最近推送
GitHub Stars
181
Forks
21
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "maui-geolocation":https://skillpi.com/zh/skills/deepskill-bc3482632e43637e398a96bc1b91b8df0251cb64437f9b496325aa1c85441bf8。安装前核对上游来源和版本:https://github.com/davidortinau/maui-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

在 .NET MAUI 应用中使用 Microsoft.Maui.Devices.Sensors 添加地理定位功能,涵盖单次定位与持续定位、各平台(Android、iOS、macOS、Windows)权限、精度级别、CancellationToken 用法、模拟位置检测与支持依赖注入的服务封装。适用于获取当前位置、持续定位与位置跟踪等场景;不包括在地图上显示位置、通用权限处理或地址地理编码。

0 下载

介绍来源: third_party · davidortinau · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: maui-geolocation
description: >
  Add geolocation capabilities to .NET MAUI apps using Microsoft.Maui.Devices.Sensors.
  Covers one-shot and continuous location, platform permissions (Android, iOS, macOS, Windows),
  accuracy levels, CancellationToken usage, mock-location detection, and a DI-friendly service wrapper.
  USE FOR: "geolocation", "GPS location", "get current location", "location permission",
  "continuous location", "location tracking", "GeolocationRequest", "mock location",
  "latitude longitude MAUI".
  DO NOT USE FOR: displaying locations on a map (use maui-maps),
  general permission handling (use maui-permissions), or geocoding addresses (use maui-maps).

.NET MAUI Geolocation

Critical: Always Pass a CancellationToken

GetLocationAsync can hang indefinitely if GPS is off, the device is indoors, or permissions are in a pending state. Always set a timeout.

// ❌ Hangs forever if no GPS fix is available
var location = await Geolocation.Default.GetLocationAsync(
    new GeolocationRequest(GeolocationAccuracy.High));

// ✅ Times out after 30 seconds
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var location = await Geolocation.Default.GetLocationAsync(
    new GeolocationRequest(GeolocationAccuracy.High), cts.Token);

Accuracy vs. Battery Trade-off

Use the lowest accuracy that satisfies your feature. Higher accuracy drains battery significantly faster — especially on Android.

Use case Recommended accuracy
City-level / weather Lowest or Low
Nearby search / store finder Medium
Turn-by-turn navigation High or Best

Platform Gotchas

iOS 14 reduced accuracy

Users can grant "approximate" location (accuracy > 1 km). Your app receives a location, but it may be useless for precision features.

  • Check location.Accuracy — values > 100 m likely indicate reduced precision.
  • Use GeolocationRequest.RequestFullAccuracy with a key matching NSLocationTemporaryUsageDescriptionDictionary in Info.plist to prompt for full accuracy.

Android: mock locations in security-sensitive flows

// ⚠️ Always check in security-sensitive flows (e.g., geofence, check-in)
if (location.IsFromMockProvider)
{
    // Reject — user is spoofing location
}

Android: Altitude 0.0 is not sea level

Some Android devices return 0.0 for Altitude when the GPS has no barometric sensor. Treat 0.0 as "unknown", not sea level.

Android 10+: background location requires separate permission

ACCESS_BACKGROUND_LOCATION must be requested separately from foreground permissions and triggers a distinct system dialog. Requesting it together with foreground permissions causes both to be denied on some devices.

GetLastKnownLocationAsync returns null

This is expected on first boot or after a location-data reset. Always fall back to GetLocationAsync:

// ✅ Cache-first with fresh fallback
var location = await geolocation.GetLastKnownLocationAsync();
if (location is null || location.Timestamp < DateTimeOffset.UtcNow.AddMinutes(-5))
{
    location = await geolocation.GetLocationAsync(
        new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10)), ct);
}

Permissions must be requested before any geolocation call

Call Permissions.RequestAsync<Permissions.LocationWhenInUse>() first, or catch PermissionException. Calling GetLocationAsync without permission throws on some platforms and returns null on others — inconsistent behaviour across platforms.


Continuous Listening: Don't Forget to Unsubscribe

Failing to remove the LocationChanged handler when stopping causes continued GPS usage and battery drain.

// ❌ Forgetting to unsubscribe — GPS stays active
public void StopTracking()
{
    Geolocation.Default.StopListeningForeground();
    // LocationChanged handler still fires!
}

// ✅ Always remove the event handler
public void StopTracking()
{
    Geolocation.Default.StopListeningForeground();
    Geolocation.Default.LocationChanged -= OnLocationChanged;
}

Checklist

  • [ ] CancellationToken passed to every GetLocationAsync call
  • [ ] Accuracy level matched to feature need (not blindly set to Best)
  • [ ] GetLastKnownLocationAsync null-check with GetLocationAsync fallback
  • [ ] Runtime permissions requested before first geolocation call
  • [ ] LocationChanged handler removed when stopping continuous listening
  • [ ] Android: ACCESS_BACKGROUND_LOCATION requested separately (if needed)
  • [ ] iOS: NSLocationTemporaryUsageDescriptionDictionary added for full-accuracy prompt
  • [ ] Android: IsFromMockProvider checked in security-sensitive flows

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

  • 提供 .NET MAUI 地理定位的完整实现指南,涵盖单次/持续定位、权限处理、精度控制、异常处理、依赖注入封装等
  • 解决开发者在跨平台定位中的常见痛点(如 CancellationToken 超时、模拟位置检测、平台差异等)
  • 属于明确场景的通用工具,受众为 .NET MAUI 开发者,实用性强
触发精准95/100

亮点

  • frontmatter 中 description 字段完整定义了适用与不适用场景
  • 覆盖多种关键词变体(geolocation、GPS location、get current location、location permission、continuous location、location tracking、GeolocationRequest、mock location、latitude longitude MAUI)
  • 边界清晰
结构规范90/100

亮点

  • 文档结构清晰,分 Critical、Accuracy vs. Battery Trade-off、Platform Gotchas、Continuous Listening、Checklist 等章节
  • 每部分有具体代码示例和说明
  • frontmatter 完整,单文件约400行
执行可靠90/100

亮点

  • 提供了关键可靠性保障(如 CancellationToken 防挂起、权限请求前置、事件处理器注销防内存泄漏、模拟位置检测、平台特定配置)
  • 覆盖主要异常场景
证据可验 (前四项均值)90/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。