Zum Hauptinhalt springen

getGeoLocation

The getGeoLocation() method accesses the device's geolocation capabilities and returns a Promise that resolves to the current geographical position. When using this function, remember that the brixxApi is instanced as app in the application context, and you should omit the await keyword even though the function is asynchronous.

Return Value

The result object contains the following structure:

{
coords: {
accuracy: 33, // Accuracy of position in meters
altitude: null, // Altitude in meters above sea level (if available)
altitudeAccuracy: null, // Accuracy of altitude (if available)
heading: null, // Direction of travel in degrees (0-360, if available)
latitude: 51.073834399999996, // Latitude in decimal degrees
longitude: 6.0768204, // Longitude in decimal degrees
speed: null // Velocity in meters per second (if available)
},
timestamp: 1557477072518 // Time when position was captured (milliseconds)
}

Usage Notes

  • This function requires user permission to access location data
  • Accuracy may vary depending on the device's hardware and connectivity
  • Some browsers only allow geolocation in secure contexts (HTTPS)
  • The function may fail in environments where geolocation services are disabled

Example Usages

Basic Usage

// Get the current location and display coordinates
const myGeoLocation = app.getGeoLocation();
console.log(myGeoLocation.coords.latitude);
console.log(myGeoLocation.coords.longitude);

Store Location in Form Fields

// Store the current location in form fields
const position = app.getGeoLocation();
app.setFieldValue('latitude', position.coords.latitude);
app.setFieldValue('longitude', position.coords.longitude);
app.setFieldValue('accuracy', position.coords.accuracy);

Open Location in Google Maps

To visualize a geo location in Google Maps, you can use the following approach:

// Open the current location in Google Maps
const myLocation = app.getGeoLocation();
window.open("http://maps.google.de/maps?q=" + myLocation.coords.latitude + "," + myLocation.coords.longitude, '_blank');

For additional Google Maps URL parameters, see this documentation.

Check if Location is Within Boundary

// Check if user is within a specified boundary
const position = app.getGeoLocation();

// Define office boundary coordinates
const officeBounds = {
minLat: 48.135125,
maxLat: 48.137125,
minLng: 11.575790,
maxLng: 11.577790
};

// Check if position is within office bounds
const isInOffice =
position.coords.latitude >= officeBounds.minLat &&
position.coords.latitude <= officeBounds.maxLat &&
position.coords.longitude >= officeBounds.minLng &&
position.coords.longitude <= officeBounds.maxLng;

if (isInOffice) {
app.setFieldValue('locationStatus', 'In Office');
} else {
app.setFieldValue('locationStatus', 'Remote');
}

Calculate Distance Between Two Points

// Calculate distance between current location and a fixed point
const position = app.getGeoLocation();
const fixedPoint = {
latitude: 50.9375,
longitude: 6.9603
};

// Calculate distance in kilometers using Haversine formula
function getDistanceInKm(lat1, lon1, lat2, lon2) {
const R = 6371; // Earth's radius in km
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLon = (lon2 - lon1) * Math.PI / 180;
const a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI/180) * Math.cos(lat2 * Math.PI/180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}

const distance = getDistanceInKm(
position.coords.latitude,
position.coords.longitude,
fixedPoint.latitude,
fixedPoint.longitude
);

app.setFieldValue('distance', Math.round(distance * 10) / 10);