Get comprehensive location context including places, geofences, regions, and neighborhoods
Radar’s Context API provides rich location context for any geographic coordinate, including nearby places, active geofences, country, state, DMA, and postal code information.
The Context API returns location information without requiring device or user identifiers, making it ideal for privacy-conscious applications and server-side integrations.
Radar.getContext { status, location, context in if status == .success, let context = context { // Geofences print("Geofences: \(context.geofences.count)") context.geofences.forEach { geofence in print(" - \(geofence.__description)") } // Place if let place = context.place { print("Place: \(place.name)") print("Categories: \(place.categories.joined(separator: ", "))") } // Country if let country = context.country { print("Country: \(country.name) (\(country.code))") if let flag = country.flag { print("Flag: \(flag)") } } // State if let state = context.state { print("State: \(state.name) (\(state.code))") } // DMA if let dma = context.dma { print("DMA: \(dma.name) (\(dma.code))") } // Postal code if let postalCode = context.postalCode { print("Postal code: \(postalCode.name)") } }}
let location = CLLocation(latitude: 40.7128, longitude: -74.0060)Radar.getContext(location: location) { status, location, context in if status == .success, let context = context { print("Location: \(location?.coordinate.latitude ?? 0), \(location?.coordinate.longitude ?? 0)") if let place = context.place { print("At place: \(place.name)") } if let country = context.country { print("In country: \(country.name)") } }}
Radar.getContext { status, location, context in if let place = context?.place { print("Place ID: \(place._id)") print("Name: \(place.name)") print("Categories: \(place.categories)") if let chain = place.chain { print("Chain: \(chain.name) (\(chain.slug))") if let chainMetadata = chain.metadata { print("Chain metadata: \(chainMetadata)") } } if let group = place.group { print("Group: \(group)") } if let address = place.address { print("Address: \(address.formattedAddress ?? "")") } // Check place properties if place.isChain("starbucks") { print("This is a Starbucks") } if place.hasCategory("restaurant") { print("This is a restaurant") } } else { print("Not at a known place") }}
Detect when a user enters specific geofences without continuous tracking:
Radar.getContext { status, location, context in let isInDeliveryZone = context?.geofences.contains { geofence in geofence.tag == "delivery-zone" } ?? false if isInDeliveryZone { print("User is in delivery zone") }}
2
Location Verification
Verify a user’s location for compliance or fraud prevention:
Radar.getContext { status, location, context in if let country = context?.country { if country.code == "US" && country.passed { print("User verified in United States") } }}
3
Place Detection
Determine if a user is at a specific type of location:
Radar.getContext { status, location, context in if let place = context?.place { if place.hasCategory("airport") { print("User is at an airport") } }}
4
Regional Customization
Customize app experience based on location:
Radar.getContext { status, location, context in if let state = context?.state { // Show state-specific content print("Showing content for \(state.name)") } if let dma = context?.dma { // Target ads by media market print("DMA: \(dma.name)") }}
Radar.getContext { status, location, context in switch status { case .success: print("Context retrieved successfully") case .errorLocation: print("Could not determine location") case .errorPermissions: print("Location permissions not granted") case .errorNetwork: print("Network error occurred") case .errorRateLimit: print("Rate limit exceeded") default: print("Error: \(Radar.stringForStatus(status))") }}
Always check if context properties are nil before using them:
if let context = context, let place = context.place { // Use place}
2
Handle empty arrays
Geofences array may be empty if the location isn’t in any geofences:
if let geofences = context?.geofences, !geofences.isEmpty { // Process geofences}
3
Respect rate limits
The Context API is subject to rate limits. Don’t call it too frequently:
// Good: Call on user actionbutton.addTarget { Radar.getContext { status, location, context in // Handle context }}// Bad: Call repeatedly in a loop// Don't do this!