HTML5 Geolocation API
Enable HTML5 geolocation in your Android WebView app and request location permissions properly.
The HTML5 Geolocation API enables users to share their location with your app, enhancing features like location-based content. In WebViewGold, the Geolocation API is supported by default once integrated into your web content. Here is how to implement Geolocation tracking into your WebView-based Android app:
Setting up HTML5 Geolocation:
For basic Geolocation setup, refer to Mozilla's Geolocation Guide for details on how to implement this feature into your website or web app.
For a demonstration, visit this W3Schools Geolocation Demo.
Enabling Background Location Tracking (Android Only):
To enable background location tracking for your WebView-based Android app, follow these steps:
- Set permissions in Config.java:
- Set
requireBackgroundLocationtotrueif background location tracking is needed. - Make sure
requireLocationis also set totrueto activate any location services.
- Set
- Adjust permissions in AndroidManifest.xml:
- Uncomment the permission blocks for
android.permission.FOREGROUND_SERVICE_LOCATIONandGPSService. - Search for these terms within the file to ensure the correct permissions are set.
- Uncomment the permission blocks for
Toggling Background Location Tracking with URL Schemes:
Use the following URL schemes to turn background location tracking on or off within your WebView-based Android app:
To turn on background location tracking, redirect the user to the following URL:
<script>window.location.href = "backgroundlocationon://"; </script>
To turn off background location tracking, redirect the user to the following URL:
<script>window.location.href = "backgroundlocationoff://"; </script>
Note: These URL schemes are specific to WebViewGold for Android and may not work in standard web browsers. To ensure these redirects are only applied for Android app users, consider detecting the User-Agent.