Asset Tracking (iOS SDK) Follow
This article describes how to implement asset tracking using the iOS SDK.
Asset Tracking
Asset Tracking is a feature that uses BLE Tags hardware to track valuable locations on a map. You can use the SDK to get Tag location updates and render Tag locations on a map.
Show All Tags on a Map
To show all tags on a map, set the showsTags MRMapView variable to true:
func showTags(mapView: MRMapView) {
mapView.showTags = true
}
Show a Specific Tag on a Map
To show a specific tag on a map, setup a predicate for that Tag’s MAC address:
func showTag(mapView: MRMapView, tag: MRTag) {
mapView.showsTags = true
mapView.tagPredicate = NSPredicate.init(format: "mac LIKE %@", tag.mac)
}
Show a Tag from Search Results
To show a tag from search results, first use the Editor API to determine which map it’s on:
func showTagFromSearchResults(tag: MRTag, appID: String) {
// MRTag returned via search doesn't contain location data.
// Use the asset-beacons API to get more info.
// Future SDK enhancements should make this explicit URL creation unnecessary.
let tagEndpoint = "https://edit.meridianapps.com/api/locations/\(appID)/asset-beacons/\(tag.mac)"
guard let url = URL(string: tagEndpoint) else {
print("Error: cannot create URL")
return
}
let task = URLSession.shared.dataTask(with: URLRequest(url: url)) {data, response, error in
if let requestError = error {
print("Tag request error: \(requestError.localizedDescription)")
return
}
guard let tagResponse = response as? HTTPURLResponse, let tagData = data else {
print("Error: did not receive tag response")
return
}
if tagResponse.statusCode != 200 {
print("Error: HTTP status code %li", tagResponse.statusCode)
return
}
do {
guard let json = try JSONSerialization.jsonObject(with: tagData, options: []) as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// Pull up the last map that a Tag was seen on.
guard let calculationDictionary = json["calculations"] as? [String: Any],
let defaultDictionary = calculationDictionary["default"] as? [String: Any],
let locationDictionary = defaultDictionary["location"] as? [String: Any],
let mapID = locationDictionary["map_id"] as? String
else {
print("Unable to retrieve location info from tag response")
return
}
let controller = MRMapViewController()
controller.mapView.mapKey = MREditorKey(forMap: mapID, app: appID)
controller.mapView.showsTags = true
controller.mapView.tagPredicate = NSPredicate.init(format: "mac LIKE %@", tag.mac)
DispatchQueue.main.async {
self.navigationController?.pushViewController(controller, animated: true)
}
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
Comments
0 comments
Please sign in to leave a comment.