Geo-Location Services
Relevant source files
The following files were used as context for generating this wiki page:- packages/core/src/services/geo-locations/GeoLocationsService.ts
- packages/common/src/entities/GeoLocation.ts
- packages/shop-mobile-ionic/src/app/pages/+invite/+by-location/by-location.page.ts
- packages/admin-web-angular/src/app/@shared/forms/google-map/google-map.component.ts
- packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts
- packages/shop-web-angular/src/app/+login/byLocation/location/location.component.ts
- packages/shop-mobile-expo/src/client/types.ts
- packages/core/src/test/integration/GeoLocationWarehouses.spec.ts
- packages/merchant-tablet-ionic/src/@shared/user/forms/location/location-form.component.ts
Geo-location services are a fundamental component of the Ever-Demand platform, enabling location-based features across various applications, including the merchant tablet, customer-facing web and mobile apps, and the admin panel. The system is responsible for capturing, storing, and processing geographical data, primarily revolving around user and warehouse addresses.
This functionality is heavily reliant on the Google Maps Platform, specifically the Geocoding API and Places API (Autocomplete), to provide address lookup, validation, and conversion between addresses and geographic coordinates (latitude and longitude). The core data structure is standardized in the GeoLocation entity, which is used consistently across the backend and various frontend applications to represent physical locations.
Core Data Model: The GeoLocation Entity
The GeoLocation entity is the central data structure for all location-based information in the platform. It is defined in the @ever-platform/common package, ensuring a consistent representation of addresses and coordinates throughout the system.
Sources: packages/common/src/entities/GeoLocation.ts, packages/shop-mobile-expo/src/client/types.ts
Entity Relationship Diagram
The diagram below shows the structure of the GeoLocation entity and its relation to the loc sub-document for storing GeoJSON point data.
Fields and Properties
The table below details the properties of the GeoLocation entity.
| Field | Type | Description |
|---|---|---|
_id | string | Unique identifier for the document. |
_createdAt | Date | Timestamp of when the document was created. |
_updatedAt | Date | Timestamp of the last update. |
countryId | Country (enum) | The ID of the country, mapped from an enum. |
city | string | The city name. |
streetAddress | string | The street name and number. |
house | string | The house or building number. |
postcode | string (optional) | The postal code. |
notes | string (optional) | Additional notes about the location. |
loc | Point (GeoJSON) | A GeoJSON Point object storing [longitude, latitude]. Used for geospatial queries in MongoDB. |
coordinates | object | A virtual getter that returns an object with { lat, lng }. |
Sources: packages/common/src/entities/GeoLocation.ts:40-119, packages/shop-mobile-expo/src/client/types.ts:15-28
The loc field is indexed as a 2dsphere to enable efficient geospatial queries, such as finding warehouses within a certain distance.
// packages/common/src/entities/GeoLocation.ts:32-34
@Schema({
index: '2dsphere'
})
Sources: packages/common/src/entities/GeoLocation.ts:32-34
Frontend Integration with Google Maps API
The frontend applications (Admin, Merchant, and Shop) heavily utilize the Google Maps API for a seamless user experience in address management. This integration is primarily handled within Angular components.
Sources: packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts, packages/shop-web-angular/src/app/+login/byLocation/location/location.component.ts, packages/merchant-tablet-ionic/src/@shared/user/forms/location/location-form.component.ts
Key Integration Points
- Autocomplete: The
google.maps.places.Autocompleteservice is attached to an input field to provide users with address suggestions as they type. - Geocoding: The
google.maps.Geocoderservice is used for two main purposes:- Forward Geocoding: Converting a user-entered address string into geographic coordinates (latitude/longitude).
- Reverse Geocoding: Converting coordinates (e.g., from a map marker) back into a human-readable address.
Address Resolution Flow
The following diagram illustrates the typical flow when a user interacts with an address form, either by typing an address or by providing coordinates.
This diagram synthesizes the logic found in _tryFindNewAddress, _tryFindNewCoordinates, and _applyNewPlaceOnTheMap functions.
Sources: packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:328-361, packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:203-222, packages/shop-web-angular/src/app/+login/byLocation/location/location.component.ts:200-244
Core Geocoding Functions
Several components across different packages share a similar set of private methods to handle the Google Maps API interaction.
| Function | Description | Example Implementation |
|---|---|---|
_initGoogleAutocompleteApi() | Initializes the google.maps.places.Autocomplete service on a search input element and sets up a listener for the place_changed event. | packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:297-314 |
_tryFindNewAddress() | Triggered on form input changes. Constructs an address string from form fields and calls the Geocoder to get coordinates. | packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:328-361 |
_tryFindNewCoordinates() | Triggered when coordinate (lat/lng) fields change. Calls the Geocoder to get a human-readable address for the given point. | packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:203-222 |
_applyNewPlaceOnTheMap() | A central handler for a PlaceResult or GeocoderResult. It extracts the geometry, updates form coordinates, and triggers address parsing. | packages/shop-web-angular/src/app/+login/byLocation/location/location.component.ts:187-217 |
_gatherAddressInformation() | Parses the address_components array from a Google Maps API result to extract individual parts like country, city, street, and house number. | packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:249-286 |
_setFormLocationValues() | Populates the Angular form controls with the parsed address components. | packages/shop-web-angular/src/app/+login/byLocation/location/location.component.ts:333-350 |
Address Management in UI Components
Location data is managed using Angular's ReactiveFormsModule. The FormBuilder service is used to create a FormGroup that mirrors the structure of the GeoLocation entity.
Sources: packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:167-181, packages/merchant-tablet-ionic/src/@shared/user/forms/location/location-form.component.ts:107-120
Example Form Definition
The following snippet shows a typical form definition for location data in the merchant tablet application.
// packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:167-179
buildForm() {
this.locationForm = this.formBuilder.group({
country: ['', Validators.required],
city: ['', Validators.required],
postcode: [''],
street: ['', Validators.required],
house: ['', Validators.required],
apartment: [''],
autodetectCoordinates: [true],
latitude: ['', Validators.required],
longitude: ['', Validators.required],
});
}
Sources: packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:167-179
This form group is then bound to the template's input fields. When a merchant updates their location, the saveChanges method is called, which prepares the Warehouse object with the new geoLocation data and sends it to the backend via the warehouseRouter.
Sources: packages/merchant-tablet-ionic/src/components/settings-page-components/location/location.ts:128-137
Backend Services
The backend services for geo-location appear to be primarily focused on data storage and retrieval.
GeoLocationsWarehousesService
The GeoLocationsWarehousesService is designed to find nearby warehouses. While the service itself is minimal, its integration tests reveal its intended purpose. It uses a get method that takes a GeoLocation object and is expected to return an Observable of nearby warehouses. The test file defines a TrackingDistance which is likely used in a geospatial query to find warehouses within a specific radius.
Sources: packages/core/src/services/geo-locations/GeoLocationsService.ts, packages/core/src/test/integration/GeoLocationWarehouses.spec.ts
Summary
The Geo-Location Services in Ever-Demand are a critical cross-cutting concern, integrating a standardized backend data model (GeoLocation) with a sophisticated frontend implementation powered by the Google Maps API. This architecture allows for a consistent and user-friendly way to manage addresses and coordinates across all platform applications. The use of a 2dsphere index on the backend enables powerful geospatial queries, such as finding nearby merchants, which is essential for an on-demand economy platform.