GET Vehicle Location - C#

GET Location within the Real-time Aggregated Data API is the most commonly used method within the REVEAL API product suite. Integrated users use this method to retrieve regular vehicle location details throughout the day to provide context to other business systems. We recommend that the user does not pull a vehicle’s location data more frequently than every 3 to 5 minutes.

Before attempting to retrieve vehicle location information it is important to know if the REVEAL account has been updated with valid VEHICLE NUMBERS for each vehicle to be pulled via the API. The VEHICLE NUMBER and VEHICLE NAME fields are different fields within REVEAL and the VEHICLE NUMBER field is not automatically populated upon account creation.

Main Method

namespace Rest_Get_Vehicle_Location_Example
{
class Program
{
static void Main()
{
// Token call goes here, see Rest_Token_Example

// App Id from Fleetmatics Integration Manager (FIM)
string appId = "companyname-p-us-4654sdf4fa351af65dsf1d";

// Vehicle Number for vehicle you wish to get the location for
string vehicleNumber = "998007";

// Insert vehicle number into URI
string getVehicleLocationUri = string.Format("https://fim.api.ENV.fleetmatics.com/rad/v1/vehicles/{0}/location", vehicleNumber);

// Call method to get vehicle location
var location = new GET_VehicleLocation().GetVehicleLocation(/* token object or auth string passed in here */, getVehicleLocationUri, appId);
}
}
}

GET_VehicleLocation Class

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Rest_Get_Vehicle_Location_Example
{
public class GET_VehicleLocation
{
public string GetVehicleLocation(/* token object or auth string passed in here */, string getVehicleLocationUri, string appId)
{
// Construct authentication header for FIM
string authHeader = string.Format("Atmosphere atmosphere_app_id={0}, Bearer {1}", appId, /* token authentication string goes here */);

// Begin new HttpClient. Use "using" to ensure the resource is released
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("applicaton/json")); // Set expected result type (application/json or application/xml)
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authHeader); // Pass in FIM auth header

// Get the response from the REST API call, passing in the endpoint to the method
using(HttpResponseMessage response = client.GetAsync(getVehicleLocationUri).Result)
{
// Get the result from the response
var result = response.Content.ReadAsStringAsync().Result;

Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); // Logging

// True if HttpStatusCode was in the Successful range (200-299); otherwise false
if (response.IsSuccessStatusCode)
{
Console.WriteLine(result); // Logging
return result; // Return Vehicle Location to parse through
}

// If this is reached, the call has failed
switch (response.StatusCode)
{
case HttpStatusCode.NotFound:
//Vehicle not found
return "";
case HttpStatusCode.Unauthorized:
//optional token renewal here, see Rest_Token_Example
return "";
/*
... other cases here
*/
default:
//take another action
return "";
}
}
}
}
}
}

Downloadable source code for this example is available as a Microsoft Visual Studio solution in a .zip file.
To download source code for this example, click HERE.

DISCLAIMER

All sample code is provided by Fleetmatics, A Verizon Company for illustrative purposes only. These examples have not been thoroughly tested under all conditions. Fleetmatics, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All programs contained herein are provided to you “AS IS” without any warranties of any kind.