I’m trying to use the GoogleAPI Roads library to snap to road.
The call is async:
GoogleMaps.Roads.SnapToRoad.QueryAsync(request).GetAwaiter();
But when I run the code I get the error ‘Operation is not supported on this platform’.
The original code was:
var response = await GoogleMaps.Roads.SnapToRoad.QueryAsync(request).Result;
But that required the void that makes that call to be async, so that just moves the problem to earlier in the callstack.
I cannot use OnCompleted either, as that requires a call to GetAwaiter first.
I’m not sure what I’m doing wrong here.
The device is running iOS 16.7.4, Xcode 15.2 and Visual Studio 17.8.6
EDIT: Updated with async task function
Still getting the same error on:
var response = await GoogleMaps.Roads.SnapToRoad.QueryAsync(request);
This is what I got now:
public static async Task<List<Position>> snapToRoad(List<Position> gpsPos)
{
List<Position> result = new List<Position>();
List<List<Position>> subLists = ListExtensions.ChunkBy<Position>(gpsPos, 100);
foreach (List<Position> gPos in subLists)
{
Coordinate[] requestPath = gPos
.Select(geo => new Coordinate((double)geo.Latitude, (double)geo.Longitude))
.ToArray();
SnapToRoadsRequest request = new SnapToRoadsRequest
{
Key = MAPS_KEY,
Path = requestPath
};
var response = await GoogleMaps.Roads.SnapToRoad.QueryAsync(request);
foreach (SnappedPoint sp in response.SnappedPoints)
{
result.Add(new Position(sp.Location.Latitude, sp.Location.Longitude));
}
}
return result;
}
It’s calld by a new thread that is started by a button click event:
async void btnSomething_Click(object sender, EventArgs e)
{
Thread trd = new Thread(async () =>
{
var snapped = await MapUtils.snapToRoad(rawCordList);
});
trd.IsBackground = true;
trd.Start();
}
2
Answers
Turns out, it was an issue with the library. It tries to set a proxy and that is not supported by Maui on iOS at this time.
Since the method returns an awaitable Task, you should
await
it and not callGetAwaiter()
or access theResult
directly. This should be done in a method that also returns an awaitable Task ("async-all-the-way", Why use Async/await all the way down):