getCurrentGeolocation( )


Returns the current geolocation of the user.

The getCurrentGeolocation() function returns a Promise that resolves to an object containing the current geolocation of the user.

The getCurrentGeolocation() function has the following limitations:

  • On Chrome, the function only works on HTTPS sites.
  • On Chrome, Firefox, and Safari, the function only works if the user approves a popup. If they do not approve, the promise is rejected.
  • Run getCurrentGeolocation() with a setTimeout() in case the browser is set to not detect the locale. Adding the timeout lets you handle the unfulfilled promise.
Method Declaration
Copy
function getCurrentGeolocation(): Promise<CurrentGeolocation>;
Request
This method does not take any parameters
Returns
Return Type:Promise<CurrentGeolocation>
Get the geolocation data
JavaScript
import { createClient } from "@wix/sdk"; import { site } from "@wix/site"; import { window as wixWindow } from "@wix/site-window"; const wixClient = createClient({ host: site.host(), modules: { wixWindow } }); (async () => { try { const obj = await wixClient.wixWindow.getCurrentGeolocation(); let timestamp = obj.timestamp; // 1495027186984 let latitude = obj.coords.latitude; // 32.0971036 let longitude = obj.coords.longitude; // 34.774391099999995 let altitude = obj.coords.altitude; // null let accuracy = obj.coords.accuracy; // 29 let altAccuracy = obj.coords.altitudeAccuracy; // null let heading = obj.coords.heading; // null let speed = obj.coords.speed; // null } catch (error) { let errorMsg = error; } })();
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?