Xamarin SDK
This is the documentation for the Xamarin package. Before integrating, read the native SDK documentation to familiarize yourself with the platform.
#
InstallInstall the package from NuGet:
Install-Package RadarIO.Xamarin
If you have an iOS and an Android project, add the package to each project.
#
Integrate#
InitializeFirst, initialize the SDK:
using RadarIO.Xamarin;using static RadarIO.Xamarin.RadarSingleton;
namespace MyProject{ public class Example { void Example () { RadarSingleton.Initialize(new RadarSDKImpl()); Radar.Initialize(publishableKey); } }}
#
Identify userTo identify the user when logged in, call:
Radar.UserId = userId;
where userId
is a stable unique ID for the user.
To set an optional dictionary of custom metadata for the user, call:
Radar.Metadata = metadata;
where metadata
is a JSONObject
with up to 16 keys and values of type string, boolean, or number.
Finally, to set an optional description for the user, displayed in the dashboard, call:
Radar.Description = description;
where description
is a string.
You only need to call these functions once, as these settings will be persisted across app sessions.
Learn about platform-specific implementations of these functions in the iOS SDK documentation and Android SDK documentation.
#
Request permissionsBefore tracking the user's location, the user must have granted location permissions for the app.
See the Xamarin Permissions docs to learn how to check and request permissions.
#
Foreground trackingOnce you have initialized the SDK and the user has granted permissions, you can track the user's location.
To track the user's location in the foreground, call:
var (status, location, events, user) = await Radar.TrackOnce();
Learn about platform-specific implementations of this function in the iOS SDK documentation and Android SDK documentation.
#
Background trackingOn iOS and Android, once you have initialized the SDK and the user has granted permissions, you can start tracking the user's location in the background.
For background tracking, the SDK supports custom tracking options as well as three presets:
TrackingOptionsEfficient
: A low frequency of location updates and lowest battery usage. On Android, avoids Android vitals bad behavior thresholds.TrackingOptionsResponsive
: A medium frequency of location updates and low battery usage. Suitable for most consumer use cases.TrackingOptionsContinuous
: A high frequency of location updates and higher battery usage. Suitable for on-demand use cases (e.g., delivery tracking) and some consumer use cases (e.g., order ahead, "mall mode").
Learn about platform-specific implementations of these presets in the iOS SDK documentation and Android SDK documentation.
To start tracking the user's location in the background, call one of:
Radar.StartTracking(TrackingOptionsEfficient);
Radar.StartTracking(TrackingOptionsResponsive);
Radar.StartTracking(TrackingOptionsContinuous);
You only need to call these methods once, as these settings will be persisted across app sessions.
Though we recommend using presets for most use cases, you can customize the tracking options. See the tracking options reference.
RadarTrackingOptions trackingOptions = TrackingOptionsContinuous;trackingOptions.DesiredStoppedUpdateInterval = 180;trackingOptions.DesiredStoppedUpdateInterval = 60;trackingOptions.DesiredSyncUpdateInterval = 50;trackingOptions.DesiredAccuracy = RadarTrackingOptionsDesiredAccuracy.High;trackingOptions.StopDuration = 140;trackingOptions.StopDistance = 70;trackingOptions.Sync = RadarTrackingOptionsSync.All;trackingOptions.Replay = RadarTrackingOptionsReplay.None;
Radar.StartTracking(trackingOptions);
To stop tracking the user's location in the background (e.g., when the user logs out), call:
Radar.StopTracking();
Learn about platform-specific implementations of these functions in the iOS SDK documentation and Android SDK documentation.
To listen for events, location updates, and errors, you can add event listeners:
Radar.ClientLocationUpdated += (result) =>{ // do something with result.location, result.stopped, result.source};
Radar.LocationUpdated += (result) =>{ // do something with result.location, result.user};
Radar.EventsReceived += (result) =>{ // do something with result.events, result.user};
Radar.Error += (err) => { // do something with err};
#
Mock trackingOn iOS and Android, you can simulate a sequence of location updates for testing. For example, to simulate a sequence of 10 location updates every 3 seconds by car from an origin
to a destination
, we can call:
Location origin = new Location(40.714708, -74.035807);Location destination = new Location(40.717410, -74.053334);
Radar.MockTracking(origin, destination, RadarRouteMode.Car, 10, 3);
#
Trip trackingOn iOS and Android, to start a trip to a destination, call:
RadarTrackingOptions tripOptions = new RadarTripOptions();tripOptions.ExternalId = "299";tripOptions.DestinationGeofenceTag = "store";tripOptions.DestinationGeofenceExternalId = "123";tripOptions.Mode = RadarRouteMode.Car;
Radar.StartTrip(tripOptions);Radar.StartTracking(TrackingOptionsContinuous);
Later, to complete the trip and stop tracking, call:
Radar.CompleteTrip();Radar.StopTracking();
Or, to cancel the trip and stop tracking, call:
Radar.CancelTrip();Radar.StopTracking();
Learn more about trip tracking.
#
Manual trackingYou can manually update the user's location by calling:
Location location = new Location(40.714708, -74.035807);location.Accuracy = 65;
var (status, location, events, user) = await Radar.TrackOnce(location);
Learn about platform-specific implementation of this function in the iOS SDK documentation and Android SDK documentation.
#
Other APIsThe Xamarin SDK also exposes APIs for anonymous context, geocoding, search, and distance.
#
Get locationGet a single location update without sending it to the server:
var (status, location) = await Radar.GetLocation();
#
ContextWith the context API, get context for a location without sending device or user identifiers to the server:
var (status, context) = await Radar.GetContext();
#
GeocodingWith the forward geocoding API, geocode an address, converting address to coordinates:
var (status, addresses) = await Radar.Geocode("20 jay st brooklyn ny");
With the reverse geocoding API, reverse geocode a location, converting coordinates to address:
Location location = new Location(40.714708, -74.035807);
var (status, addresses) = await Radar.ReverseGeocode(location);
With the IP geocoding API, geocode the device's current IP address, converting IP address to city, state, and country:
var (status, address, proxy) = await Radar.IpGeocode();
#
SearchWith the autocomplete API, autocomplete partial addresses and place names, sorted by relevance:
Location near = new Location(40.783826, -73.975363);
var (status, addresses) = await Radar.Autocomplete("brooklyn roasting", near, 10);
With the geofence search API, search for geofences near a location, sorted by distance:
Location near = new Location(40.783826, -73.975363);
var (status, geofences) = await Radar.SearchGeofences(near, 1000, new string[]{ "venue" }, 10);
With the places search API, search for places near a location, sorted by distance:
Location near = new Location(40.783826, -73.975363);
var (status, places) = await Radar.SearchPlaces(near, 1000, new string[]{ "starbucks" }, null, null, 10);
#
DistanceWith the distance API, calculate the travel distance and duration from an origin to a destination:
Location origin = new Location(40.78382, -73.97536);Location destination = new Location(40.70390, -73.98670);
var (status, routes) = await Radar.GetDistance(origin, destination, new[] { RadarRouteMode.Foot, RadarRouteMode.Car }, RadarRouteUnits.Imperial);
#
MatrixWith the matrix API, calculate the travel distances and durations between multiple origins and destinations for up to 25 routes:
var (status, routes) = await Radar.GetDistance(origins, destinations, RadarRouteMode.Car, RadarRouteUnits.Imperial);
#
SupportHave questions? We're here to help! Email us at support@radar.com.