Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/geolocation/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:hasCode="true"
android:supportsRtl="true"
android:icon="@mipmap/ic_launcher"
android:extractNativeLibs="true"
android:allowNativeHeapPointerTagging="false"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
>
<activity
android:configChanges="orientation|screenLayout|screenSize|keyboardHidden"
android:exported="true"
android:label="@string/app_name"
android:name="dev.dioxus.main.MainActivity"
>
<meta-data
android:name="android.app.lib_name"
android:value="dioxusmain"
/>
<meta-data
android:name="android.app.func_name"
android:value="ANativeActivity_onCreate"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3 changes: 2 additions & 1 deletion examples/geolocation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ edition = "2021"
publish = false

[dependencies]
dioxus = { workspace = true }
dioxus = "0.7.0-alpha0"
dioxus-sdk-geolocation = { workspace = true }

[features]
default = ["desktop"]
web = ["dioxus/web"]
desktop = ["dioxus/desktop"]
mobile = ["dioxus/mobile"]

2 changes: 2 additions & 0 deletions examples/geolocation/Dioxus.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[application]
android_manifest = "./AndroidManifest.xml"
4 changes: 4 additions & 0 deletions packages/geolocation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ windows = { version = "0.48.0", features = [
"Foundation",
"Devices_Geolocation",
] }

[target.'cfg(target_os = "android")'.dependencies]
dioxus = { workspace = true, features = ["mobile"] }
jni = "0.21.1"
28 changes: 26 additions & 2 deletions packages/geolocation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Geolocation utilities and hooks for Dioxus.
### Supports
- [x] Web
- [x] Windows
- [x] Android (draft)
- [ ] Mac
- [ ] Linux
- [ ] Android
- [ ] iOs
- [ ] iOS

## Usage
Add `dioxus-sdk-geolocation` to your `Cargo.toml`:
Expand Down Expand Up @@ -41,3 +41,27 @@ fn App() -> Element {
}
}
```

## Platform Notes

### Android

> **Note:** Android support requires `dioxus = "0.7.0-alpha0"` or later.

The Android implementation provides robust geolocation support via JNI:

**Features:**
- **Automatic permission handling** - Requests `ACCESS_FINE_LOCATION` (GPS) or `ACCESS_COARSE_LOCATION` (network) based on `PowerMode`
- **Power mode support** - `PowerMode::High` uses GPS provider, `PowerMode::Low` uses network provider
- **Continuous location updates** - Background polling with configurable intervals (1s for High, 5s for Low accuracy)
- **Change detection** - Only emits `NewGeocoordinates` events when position changes (0.00001 degree threshold)
- **Permission monitoring** - Detects permission revocation during active listening
- **Automatic cleanup** - Listener thread stops when `Geolocator` is dropped

**Required Permissions:**

Add to your `AndroidManifest.xml`:
```xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
```
2 changes: 1 addition & 1 deletion packages/geolocation/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Geocoordinates {
}

/// To conserve battery, some devices allow setting a desired accuracy based on your use-case.
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum PowerMode {
/// Will generally enable the on-board GPS for precise coordinates.
High,
Expand Down
Loading