Location Access Plugin
Smart WebView’s LocationPlugin provides a modern, secure, and battery-efficient way to access the device’s location from your web application.
Enabling Location Services
- Enable in Configuration: In
swv.properties, ensure theLOCATIONpermission group is requested on launch.# swv.properties permissions.on.launch=NOTIFICATIONS,LOCATION - Enable the Plugin: Make sure
LocationPluginis in theplugins.enabledlist.# swv.properties plugins.enabled=LocationPlugin,ToastPlugin,...
Permissions
The app declares and requests ACCESS_FINE_LOCATION. The user must grant this permission at runtime for the feature to work.
How it Works
The LocationPlugin provides a JavaScript interface that your web code can call on demand. This is more efficient than constantly tracking the user’s location.
- JavaScript Call: Your web app calls
window.SWVLocation.getCurrentPosition(), passing a callback function. - Native Request: The plugin receives the request and asks the Android system for the current location.
- Timeout & Cache Fallback: The plugin waits up to 10 seconds for a GPS fix. If no result is returned in time, it automatically falls back to the last known cached location (via
GeolocationCachePlugin). This ensures your web app always receives some coordinates even in poor signal conditions. - Callback Execution: Once the location is retrieved (or if an error occurs), the plugin executes your JavaScript callback, passing the latitude, longitude, and any error message as arguments.
Accessing Coordinates in JavaScript:
This is the recommended way to get location data.
// Check if the location feature is available
if (window.SWVLocation) {
// Request the current position
window.SWVLocation.getCurrentPosition(function(lat, lng, error) {
if (error) {
console.error("Location Error:", error);
// e.g., display an error message to the user
return;
}
console.log(`Latitude: ${lat}, Longitude: ${lng}`);
// Use the coordinates in your web app
// e.g., show a marker on a map
});
} else {
console.log('Location feature not available.');
}
The JavaScript object is window.SWVLocation, not window.Location. This is to avoid a critical conflict with the browser’s built-in window.location object.
Required Plugins
LocationPlugin depends on GeolocationCachePlugin to store and retrieve cached coordinates. Ensure both are listed in plugins.enabled in swv.properties:
plugins.enabled=LocationPlugin,GeolocationCachePlugin,...
If GeolocationCachePlugin is absent, the cache fallback will be silently skipped and only a live GPS fix will be attempted.