Capturing GPS coordinates.

522
2
Jump to solution
01-11-2023 05:20 PM
DuanePfeiffer
New Contributor III

I need to capture several GPS points and then average them.  In version 100, I accomplished this by adding an observer to locationDisplay for keyPath:location.  Not sure how to do this in version 200.  Is there an example?  From what I can tell, I need to add a callback handler to the LocationDataSource object?  If so, how do I do that?

From Documentation:

var locations: AsyncStream<Location>

Set the location changed callback function for the location data source.
0 Kudos
1 Solution

Accepted Solutions
Ting
by Esri Contributor
Esri Contributor

Please see the attached demo. Use a moving simulated location on Simulator (e.g., Features > Location > City Run) to see how it works.

The key is to understand the AsyncStream concept in the relatively new Swift Concurrency.

As you noticed, the doc of LocationDataSource.locations shows its type as AsyncStream<Location>. That means you can use the for-await-in syntax to process its updates as the stream produces it, such as 

 

for await newLocation in locationDisplay.dataSource.locations {
    // currentLocation = newLocation
}

 

Unfortunately I haven't written a sample for it yet, but in the upcoming months a few more examples regarding the asynchronous stream pattern will be added to the sample viewer.

Furthermore, we are planning to publish a series of blogposts to introduce the various new concepts that come along with the new ArcGIS Maps SDK for Swift together with SwiftUI and Swift Concurrency, etc. Stay tuned!

p.s. the doc is not accurate, and we will reword it in the future.

View solution in original post

0 Kudos
2 Replies
Ting
by Esri Contributor
Esri Contributor

Please see the attached demo. Use a moving simulated location on Simulator (e.g., Features > Location > City Run) to see how it works.

The key is to understand the AsyncStream concept in the relatively new Swift Concurrency.

As you noticed, the doc of LocationDataSource.locations shows its type as AsyncStream<Location>. That means you can use the for-await-in syntax to process its updates as the stream produces it, such as 

 

for await newLocation in locationDisplay.dataSource.locations {
    // currentLocation = newLocation
}

 

Unfortunately I haven't written a sample for it yet, but in the upcoming months a few more examples regarding the asynchronous stream pattern will be added to the sample viewer.

Furthermore, we are planning to publish a series of blogposts to introduce the various new concepts that come along with the new ArcGIS Maps SDK for Swift together with SwiftUI and Swift Concurrency, etc. Stay tuned!

p.s. the doc is not accurate, and we will reword it in the future.

0 Kudos
Ting
by Esri Contributor
Esri Contributor

update: doc will be updated in next release.

0 Kudos