Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

GH-272 Fix Null Exception on Location Updates #276

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Xamarin.Essentials/Geolocation/Geolocation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ static async Task<Location> PlatformLastKnownLocationAsync()
bestLocation = location;
}

if (bestLocation == null)
return null;

return bestLocation.ToLocation();
return bestLocation?.ToLocation();
}

static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
Expand Down
9 changes: 3 additions & 6 deletions Xamarin.Essentials/Geolocation/Geolocation.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static async Task<Location> PlatformLastKnownLocationAsync()
var manager = new CLLocationManager();
var location = manager.Location;

return location.ToLocation();
return location?.ToLocation();
}

static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -49,10 +49,7 @@ static async Task<Location> PlatformLocationAsync(GeolocationRequest request, Ca

var clLocation = await tcs.Task;

if (clLocation == null)
return null;

return clLocation.ToLocation();
return clLocation?.ToLocation();

void HandleLocation(CLLocation location)
{
Expand Down Expand Up @@ -81,7 +78,7 @@ public override void LocationsUpdated(CLLocationManager manager, CLLocation[] lo

wasRaised = true;

var location = locations.LastOrDefault();
var location = locations?.LastOrDefault();

if (location == null)
return;
Expand Down
16 changes: 2 additions & 14 deletions Xamarin.Essentials/Geolocation/Geolocation.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ static async Task<Location> PlatformLastKnownLocationAsync()

var location = await geolocator.GetGeopositionAsync().AsTask();

if (location?.Coordinate == null)
return null;

return location.ToLocation();
return location?.Coordinate?.ToLocation();
}

static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
Expand All @@ -39,16 +36,7 @@ static async Task<Location> PlatformLocationAsync(GeolocationRequest request, Ca

var location = await geolocator.GetGeopositionAsync().AsTask(cancellationToken);

if (location?.Coordinate == null)
return null;

return new Location
{
Latitude = location.Coordinate.Point.Position.Latitude,
Longitude = location.Coordinate.Point.Position.Longitude,
TimestampUtc = location.Coordinate.Timestamp,
Accuracy = location.Coordinate.Accuracy
};
return location?.Coordinate?.ToLocation();
}
}
}
17 changes: 13 additions & 4 deletions Xamarin.Essentials/Types/LocationExtensions.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ internal static IEnumerable<Location> ToLocations(this MapLocationFinderResult r
result?.ToLocations();

internal static Location ToLocation(this Geoposition location) =>
new Location
{
Latitude = location.Coordinate.Point.Position.Latitude,
Longitude = location.Coordinate.Point.Position.Longitude,
TimestampUtc = location.Coordinate.Timestamp,
Accuracy = location.Coordinate.Accuracy
};

internal static Location ToLocation(this Geocoordinate coordinate) =>
new Location
{
Latitude = location.Coordinate.Point.Position.Latitude,
Longitude = location.Coordinate.Point.Position.Longitude,
TimestampUtc = location.Coordinate.Timestamp,
Accuracy = location.Coordinate.Accuracy
Latitude = coordinate.Point.Position.Latitude,
Longitude = coordinate.Point.Position.Longitude,
TimestampUtc = coordinate.Timestamp,
Accuracy = coordinate.Accuracy
};
}
}