Documentation
This tutorial used to build a BlackBerry 10 app that showed the device's location when you pressed a jqxButton. BlackBerry 10 is retired: BlackBerry ended its services in January 2022, and Cordova no longer supports the platform. The same app now runs as a plain web page in the browser of any current phone, with no native wrapper.
Browsers have a standard Geolocation API, and PhoneGap's geolocation plugin was modelled on it. A page served over HTTPS can ask for the location, and the browser shows the permission prompt.
Wrap the page in Capacitor only when you need an app store listing or device features the browser does not offer; see A location app for Android.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Where am I?</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.base.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.material.css">
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxcore.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxbuttons.js"></script>
</head>
<body>
<button id="locateButton" type="button">Show my location</button>
<p id="result" aria-live="polite"></p>
<script src="locate.js"></script>
</body>
</html>
aria-live makes screen readers announce the result when it appears.
locate.js creates the button and asks for the position only when the user taps it. Browsers may block location prompts that appear without a user action, and users are more likely to allow a request they started.
$('#locateButton').jqxButton({ width: 200, height: 44, theme: 'material' });
$('#locateButton').on('click', function () {
if (!('geolocation' in navigator)) {
$('#result').text('This browser cannot report a location.');
return;
}
$('#result').text('Finding your location…');
navigator.geolocation.getCurrentPosition(
function (position) {
$('#result').text(
'Latitude ' + position.coords.latitude.toFixed(5) +
', longitude ' + position.coords.longitude.toFixed(5) +
' (within ' + Math.round(position.coords.accuracy) + ' m)'
);
},
function (error) {
const messages = {
1: 'Location access was denied. Allow it in the browser settings for this site.',
2: 'The location is not available right now.',
3: 'Finding the location took too long. Try again.'
};
$('#result').text(messages[error.code] || error.message);
},
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 60000 }
);
});
maximumAge lets the browser reuse a position up to a minute old, which saves battery when the user taps the button repeatedly.
Browsers expose the Geolocation API only in secure contexts: pages served over HTTPS, or from http://localhost while you develop. On a plain http:// address, getCurrentPosition fails with a permission error.
To test on a phone during development, serve the page over HTTPS, for example with a Vite dev server and a local certificate, or use the browser's device emulation with a simulated location; see Test and debug Capacitor apps.