Device Location

Get GPS coordinates from the native app

Inside the WebToApp app, request GPS through the JavaScript bridge instead of navigator.geolocation. The native layer uses the device GPS and returns coordinates via a custom DOM event.

When to use

Use the bridge when window.FlutterWebView is defined. In a normal browser, use navigator.geolocation instead.

Request location

Send a message with a unique requestId so you can match the response:

const requestId = 'loc-' + Date.now();

window.addEventListener('FlutterLocationResponse', (event) => {
  const detail = event.detail;
  if (detail.requestId !== requestId) return;

  if (detail.success) {
    const { lat, lng, accuracy, timestamp } = detail.data;
    console.log(lat, lng);
  } else {
    console.error(detail.error.message);
  }
});

window.FlutterWebView.postMessage(JSON.stringify({
  type: 'getLocation',
  requestId,
  options: {
    highAccuracy: true,
    timeoutMs: 10000
  }
}));

Response format

Listen for the FlutterLocationResponse event. On success, detail.data contains lat, lng, accuracy, and timestamp. On failure, detail.success is false and detail.error describes the problem.

Permissions

Location permissions are included in every app build. The first request may show the system permission dialog. If the user denies access, handle the error in your UI and explain why location is needed.