/// Get a channel controller for the channel.
let channelController = chatClient.channelController(for: ChannelId(type: .messaging, id: "general"))
/// Send a message with a static location.
let location = LocationInfo(latitude: 10, longitude: 10)
channelController.sendStaticLocation(location)
/// Alternatively, you can use the create new message with a `NewLocationInfo` object and endAt as nil.
let newLocation = NewLocationInfo(latitude: 10, longitude: 10, endAt: nil)
let message = channelController.createNewMessage(text: "", location: newLocation)
Location Sharing
Location sharing allows users to send a static position or share their real-time location with other participants in a channel. Stream Chat supports both static and live location sharing through location attachments.
There are two types of location sharing:
- Static Location: A one-time location share that does not update over time.
- Live Location: A real-time location share that updates over time.
The SDK handles location message creation and updates, but location tracking must be implemented by the application using device location services.
Sending static location
Static location sharing allows you to send a message containing a static location.
Starting live location sharing
Live location sharing enables real-time location updates for a specified duration. The SDK manages the location message lifecycle, but your application is responsible for providing location updates.
/// Get a channel controller for the channel.
let channelController = chatClient.channelController(for: ChannelId(type: .messaging, id: "general"))
/// Send a message with a live location with the initial coordinates.
let location = LocationInfo(latitude: 10, longitude: 10)
channelController.startLiveLocationSharing(location)
/// Alternatively, you can use the create new message with a `NewLocationInfo` object and endAt non-nil.
let oneMinuteFromNow = Date().addingTimeInterval(60)
let newLocation = NewLocationInfo(latitude: 10, longitude: 10, endAt: oneMinuteFromNow)
let message = channelController.createNewMessage(text: "", location: newLocation)
Stopping live location sharing
You can stop live location sharing for a specific message using the message controller:
/// Get the message controller for the live location message
let messageController = chatClient.messageController(
cid: channelId,
messageId: liveLocationMessageId
)
/// Stop live location sharing
messageController.stopLiveLocationSharing()
Updating live location
Your application must implement location tracking and provide updates to the SDK. The SDK handles updating all the current user’s active live location messages and provides a throttling mechanism to prevent excessive API calls.
/// Get the current user controller.
let currentUserController = chatClient.currentUserController()
/// Set delegate to receive live location updates.
currentUserController.delegate = self
/// Load active live locations (call only once at app start or when chat is initialized)
currentUserController.loadActiveLiveLocationMessages()
/// Update location when device location changes.
func locationDidUpdate(_ newLocation: LocationInfo) {
currentUserController.updateLiveLocation(newLocation)
}
Whenever the location is updated, the message will automatically be updated with the new location.
The SDK will also notify your application when it should start or stop location tracking as well as when the active live location messages change.
extension SomeObject: CurrentChatUserControllerDelegate {
/// Called when the user starts sharing a live location and it wasn't already sharing a live location
func currentUserControllerDidStartSharingLiveLocation(
_ controller: CurrentChatUserController
) {
// Start location monitoring (Needs to be implemented by the application)
startLocationTracking()
}
/// Called when all live location sharing stops
func currentUserControllerDidStopSharingLiveLocation(
_ controller: CurrentChatUserController
) {
// Stop location monitoring
stopLocationTracking()
}
/// Called when active live location messages change
func currentUserController(
_ controller: CurrentChatUserController,
didChangeActiveLiveLocationMessages messages: [ChatMessage]
) {
// You can use this delegate method to track the active live location messages and
// if needed render all of them in a UI component or you can use this for finer control
// when to start or stop tracking locations.
}
/// Called when a live location update fails
func currentUserController(
_ controller: CurrentChatUserController,
didFailToUpdateLiveLocation location: SharedLocation,
with error: Error
) {
// Handle error if needed
}
}
Events
Whenever a location is created or updated, the following WebSocket events will be sent:
message.new
: When a new location message is created.message.updated
: When a location message is updated.
You can easily check if a message is a location message by checking the message.sharedLocation
property. For example, you can use this events to render the locations in a map view.
let eventsController = ChatClient.shared.eventsController()
eventsController.delegate = self
extension CustomMapView: EventsControllerDelegate {
func eventsController(_ controller: EventsController, didReceiveEvent event: any Event) {
/// Make sure the event is for the current channel.
guard event.cid == currentChannelId else {
return
}
if let event = event as? MessageNewEvent, let sharedLocation = event.message.sharedLocation {
// Add the new location to the map.
} else if let event = event as? MessageUpdatedEvent, let sharedLocation = event.message.sharedLocation { {
// Update the location on the map.
}
}
}