mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 05:51:09 -05:00
3.1 KiB
3.1 KiB
Location Model
Overview
The Location model provides polymorphic location management for parks, areas, and other entities in ThrillWiki. It handles geocoding, coordinate management, and location-based search capabilities.
Structure
Database Table
- Table Name:
locations - Primary Key:
id(bigint) - Polymorphic Fields:
locatable_type,locatable_id - Timestamps:
created_at,updated_at
Fields
-
Address Components
address(string, nullable) - Street addresscity(string) - City namestate(string, nullable) - State/provincecountry(string) - Country namepostal_code(string, nullable) - Postal/ZIP code
-
Coordinates
latitude(decimal, 10,8) - Latitude coordinatelongitude(decimal, 11,8) - Longitude coordinateelevation(decimal, 8,2, nullable) - Elevation in meters
-
Additional Details
timezone(string, nullable) - Location timezonemetadata(json, nullable) - Additional location datais_approximate(boolean) - Indicates if location is approximatesource(string, nullable) - Data source identifier
-
Geocoding
geocoding_data(json, nullable) - Cached geocoding responsegeocoded_at(timestamp, nullable) - Last geocoding timestamp
Indexes
- Coordinates:
(latitude, longitude) - Location:
(country, state, city) - Postal:
postal_code
Relationships
Polymorphic
locatable()- Polymorphic relationship to parent model (Park, Area, etc.)
Accessors & Mutators
coordinates- Returns [lat, lng] arrayformatted_address- Returns formatted address stringmap_url- Returns Google Maps URL
Methods
Location Management
updateCoordinates(float $lat, float $lng)- Update coordinatessetAddress(array $components)- Set address componentsgeocode()- Trigger geocoding refreshreverseGeocode()- Get address from coordinates
Queries
scopeNearby($query, $lat, $lng, $radius)- Find nearby locationsscopeInBounds($query, $ne, $sw)- Find locations in boundsscopeInCountry($query, $country)- Filter by countryscopeInState($query, $state)- Filter by statescopeInCity($query, $city)- Filter by city
Calculations
distanceTo($lat, $lng)- Calculate distance to pointbearingTo($lat, $lng)- Calculate bearing to point
Usage Examples
// Create location for park
$park->location()->create([
'address' => '123 Main St',
'city' => 'Orlando',
'state' => 'FL',
'country' => 'USA',
'latitude' => 28.538336,
'longitude' => -81.379234
]);
// Find parks within 50km
$nearbyParks = Park::whereHas('location', function ($query) {
$query->nearby(28.538336, -81.379234, 50);
})->get();
Integration Points
Services
- GeocodeService - Address/coordinate lookup
- LocationSearchService - Advanced location search
Components
- LocationSelector - Map-based location picker
- LocationDisplay - Location visualization
Notes
- Coordinates use high precision for accuracy
- Geocoding results are cached to reduce API calls
- Polymorphic design allows reuse across models
- Search methods use spatial indexes for performance