Field Staff Tracking
Mobile Attendance
Geolocation & Reverse Geocoding

Geolocation & Reverse Geocoding

The system converts raw GPS coordinates into human-readable addresses using the Google Maps API. This powers the location display in the admin portal and approval screens.

Reverse Geocoding Flow

Two-Tier Geocoding Strategy

The system uses a two-tier approach for maximum accuracy:

Tier 1: Google Places Nearby Search (V2)

GET https://maps.googleapis.com/maps/api/place/nearbysearch/json
    ?location={lat},{lng}
    &radius=50
    &key={GoogleAPIKey}

This searches within 50 meters for named places, filtering out generic "route" results. The first non-route result's Name and Vicinity are combined for the location string.

Tier 2: Google Geocoding API (Fallback)

GET https://maps.googleapis.com/maps/api/geocode/json
    ?latlng={lat},{lng}
    &key={GoogleAPIKey}
    &result_type=premise|point_of_interest

If the nearby search returns no usable results, the system falls back to standard reverse geocoding, preferring "premise" type results. Plus codes are stripped from the formatted address for cleaner output.

Plus Code Handling

Google's geocoding API sometimes returns Plus Codes (e.g., WXYZ+AB) in the formatted address. The system detects and removes these:

int PlusCodeIndex = address_components.FindIndex(
    a => a.types.Contains("plus_code"));
if (PlusCodeIndex >= 0)
{
    FormattedAddress = FormattedAddress
        .Replace(ReplaceThisString + " ", "")
        .Replace(ReplaceThisString + ", ", "")
        .Replace(ReplaceThisString, "");
}

Compound Code Fallback

When no standard results are available but a plus_code.compound_code exists, the system extracts the human-readable portion after the first space:

// Input: "WXYZ+AB Chennai, Tamil Nadu, India"
// Output: " Chennai, Tamil Nadu, India"
int indx = CompoundCode.IndexOf(' ');
GeoLocationName = CompoundCode.Substring(indx, CompoundCode.Length - indx);

Configuration

SettingSourceDescription
GoogleAPIKeySHApplicationSettingAPI key for Google Maps services
TLS ProtocolSecurityProtocolType.Tls12Enforced TLS 1.2 for API calls
Nearby Radius50 metersSearch radius for places API
Result Typespremise, point_of_interestPreferred geocoding result types

Error Handling

The geocoding system logs detailed error information including the requested URL and raw response for debugging API failures:

ApplicationSettings.GetAppLogger().WriteLog(
    $"Error : Lat - {lat} Lng - {lng}", LogType.LogTypeError);
ApplicationSettings.GetAppLogger().WriteLog(
    $"Requested URL : {requestedURL}", LogType.LogTypeError);
ApplicationSettings.GetAppLogger().WriteLog(
    $"Result Received : {result}", LogType.LogTypeError);

Source Files

FilePurpose
GeoLocationBO.csAll geocoding logic
GooglePlaceResponseNearby search response model
GeoLocationAPIModelGeocoding API response model