Location Tracking
Geolocation and tracking functionality continues to advance at an impressive pace. We’ve moved beyond static maps to interactive, dynamic mapping technology. Real-time geolocation tracking plays an important role in many on-demand services such as taxis, food delivery apps, and fleet monitoring. The real value of tracking isn’t just knowing live location, it’s also the insights you collect and analyze over time—the ability to pair historical data with a real-time view of your operations.
PubNub enables your users to stream location data to other users within your application. Use our pub/sub messaging to display driver location on real-time maps, or use geolocation for provisioning events for orders and drivers.
Features
Live Lat/Long Location
Location updates (containing an ID, latitude, longitude, and time stamp at a minimum) are what your devices collect and send, providing a snapshot in time for each asset. A good example is a driver at a ride-sharing company using a driver app equipped with in-app navigation. When using the app, drivers transmit origin/destination information, route paths, or vehicle health data. All of these location updates will be shared with another device or a server via an intermediary. PubNub fills that spot.
Sharing Location Updates
The Lat/Long coordinates can be sent in any channels either as a signal or as a regular message. In most location-sharing cases, a signal will be the right choice. Signals vary from a regular message in size, SLA, pricing, and some functionality; refer to Messages vs Signals to learn more.
In the following example, we use Signals to illustrate how to use PubNub to share a location. We get the new location in a marker
variable and are sent to a geolocation_channel
to any subscriber who is currently listening on that channel. Since Signals are limited to send 30 bytes only, we format the coordinate values up to 6 digits after the decimal point, which is the same format that Google Maps uses.
// Get coordinate values up to 6 decimal numbers
DecimalFormat decimalFormat = new DecimalFormat("0.######");
JsonArray payload = new JsonArray();
payload.add(decimalFormat.format(marker.getPosition().latitude)); // Get lat coord.
payload.add(decimalFormat.format(marker.getPosition().longitude)); // Get long coord.
// Message Payload size for PubNub Signals is limited to 30 bytes
pubnub.signal()
.channel("geolocation_channel")
.message(payload)
.async(new PNCallback < PNPublishResult > () {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
// Error
if (status.isError()) {
System.out.println("Error- pub status code: " + status.getStatusCode());
}
}
});
To learn more about how PubNub can help with reliably sharing locations, please visit:
Live Location Tracking
You can track the live location of flights, drivers, and other IoT devices in real time on maps. This can help eliminate "where's my driver" support calls. Once users start sharing their location, the Lat/Long coordinates are published periodically to a channel. The location marker on your map can be updated for every user and device connected to your application. You can format the coordinate values up to 6 decimal places, which is the format used by Google Maps and many other mapping APIs.
Set Up Google Maps Event Listener
This method is triggered when Google Maps is ready to use.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Receive payload from a subscribe call
// Initial coordinates in S.F.♥︎
JsonArray payload = new JsonArray();
payload.add(37.782486);
payload.add(-122.395344);
placeMarker(payload);
}
To learn more about using PubNub to load locations on a map, check out:
Proximity Search
Route location updates to a NoSQL database for storage, where it can be quickly accessed using the front-end interface. Any special properties added with Functions (such as distance from destination) can be stored as additional fields. Now, users can search the database for devices based on proximity; for example, to find taxis close to their location.
To learn more about how PubNub can help with proximity searching, please visit: