Getting Started with LocationKit for iOS and Android

Many modern mobile apps rely heavily on GPS and user location data, whether it is part of an app's core functionality--such as with a navigation app--or is used more indirectly--such as for analytical and marketing purposes. There are few companies that can't benefit from contextual location user information, but it often comes at the cost of rapid battery degradation when using the default iOS or Android SDK location libraries. Enter SocialRadar's LocationKit, a direct replacement for these libraries that not only adds functionality but also reduces battery drain.

LocationKit will be a boon for companies that want to add location-based capabilities to their apps but don't have the development resources to build the features out themselves. As a direct replacement SDK, LocationKit inherits all of the features of CLLocationManager (iOS) and LocationManager (Android), but introduces a plethora of additional features.

Further, with its large database of venues, buildings and addresses, LocationKit saves on calls. After automatically detecting a significant place visit, it fetches all the contextual information:

let locationManager = LKLocationManager() locationManager.requestPlace() {(place: LKPlace?, error: NSError?) in
  if let place = place {
     print("The user is in: (\(place.locality))")
  }
}

Here are some of the most interesting and useful features in LocationKit:

Activity Modes

LocationKit can determine the current mode of activity the user is in--whether the user is walking, driving, at home or at work--by using raw data from sensors such as the GPS and accelerometer. The standard Apple and Android SDKs don't provide a sensory way of determining the mode the user is in. If your app has been authorized access to the iOS CoreMotion manager (which utilizes the motion coprocessor), LocationKit will switch over to using that instead of the raw sensors, This will save battery life and provide higher accuracy for activity mode tracking.

On iOS, you would listen for such activity as follows:

func locationManager(manager: LKLocationManager, willChangeActivityMode mode: LKActivityMode) {
  if (mode == LKActivityMode.Automotive) {
    print("The user is likely driving right now")
  } else {
    print("The user is likely NOT driving right now")
  }
}

People Nearby

The People Nearby feature makes it possible to detect other users of your app who are nearby. "Nearby" is a fluid term, according to LocationKit, but in general it refers to a city block. When an "LKPerson object" is returned, limited information about others users in range is made available. Enabling this feature in an app lets companies, for example, initiate marketing efforts or connect users.

LocationKit notes that user information is not shared with other users for security and privacy reasons. On iOS, developers would look for other nearby users, as follows:

locationManager.requestPeopleAtCurrentPlace { (people: [LKPerson]?, venue:LKVenue?, error:NSError) -> Void in
  if let people = people {
    print("There are \(people.count) people at \(venue.name) with you")
  } else {
    print("Sorry, no people found at \(venue.name) with you")
  }
}

Automatic Venue Detection

LocationKit can automatically detect when a user is at a venue, using Storefront Map Database. This visit monitoring capability detects when a user enters or leaves a particular place--say, Starbucks on 2nd and Market Street. LocationKit's SDK provides two delegate methods--didStartVisit and didEndVisit--to notify the system of those locational particulars:

func locationManager(manager: LKLocationManager, didStartVisit visit: LKVisit) {
  // Print out the street number and street name of the place where this
  // visit started
  print("Started visit at \(visit.place.subThoroughfare), \(visit.place.thoroughfare)")
}

There are many possible applications for this feature. For example, companies could use it to provide a promotion screen when a user enters a certain location--such as offering a 20% off coupon when a user enters a Starbucks between 3 p.m. an 5 p.m., or presenting a parking reservation ticket when a user enters a particular concert venue. This is accomplished regardless of whether the app is in the foreground or background, using a predictive algorithm that uses multiple sensors to pinpoint location. 

Android and iOS do provide standard geofencing capabilities, but LocationKit goes above and beyond by providing contextually aware place/venue information without extra reverse geocoding calls, while maintaining efficient battery-usage of the phone. image

Analytics Dashboard

LocationKit provides a robust dashboard through which a wide range of data is visualized.

For example, leveraging LocationKit’s ability to extrapolate key locations, business managers could see at a glance means when the app is being used at home and when it's used at work. you get to have a great graphical display of where your users live work, and shop. This, of course, improves companies' ability to improve “customer acquisition, app engagement and user retention."

Business managers can also have their dashboard broken down by segment--such as gender, age and income level--unlocking great customer insights without much work on the developer's part. In fact, that's the benefit of using LocationKit's analytics tool over any other popular tool, such as Google Analytics or Crashlytics: You are leveraging other LocationKit SDK features, so most of the coding effort has already been done. The only additional work developers need to do would be to pass custom event data to LocationKit's backend.

Slide12.jpgSlide41.jpg
As you can see, there are some real benefits to replacing your existing default Location Manager with something that can provide greater insight while not sacrificing performance and battery life. Next up, we will show you how easy it is to integrate LocatinKit into your iOS or Android app.

Getting Started with LocationKit SDK

Regardless of which platform you will work with, the first thing you need to do is sign up, after which you should receive your API token via the Developer Dashboard. The next steps are specific to each platform, so we will start with iOS.

iOS

The easiest way to get started on iOS, is with CocoaPods, adding the following to your Podfile:

pod 'LocationKit', '~> 3.0'

Then, check in Settings > Background Modes, that Location updates is enabled.
uezVNpFAQ7yByBK91FS2
Next, in your info.plist file, add the following privacy and permission-related attributes:

NSLocationAlwaysUsageDescription
Privacy - Location Usage Description

Finally, if you don’t have one already, create an objective-c bridging header, and add the following to reference the LocationKit Objective-C library:

#import <LocationKit/LocationKit.h>

To start using LocationKit in your app thereafter, you simply need to add the import statement to the top of your file:

import LocationKit

To start lKLocationManager immediately at launch, delegate file’s didFinishLaunchingWithOptions method, and add:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  let locationManager = LKLocationManager()
  locationManager.debug = true //false if you are in production mode
  locationManager.apiToken = "YourToken"
  locationManager.startUpdatingLocation()

  return true
}

Getting a Location

The simplest way to get the current location is to use the requestLocation handler method:

self.locationManager.requestLocation { (location: CLLocation?, error: NSError?) -> Void in
    // We have to make sure the location is set, could be nil
    if let location = location {
        print("You are currently at: \(location)")
    }
}

Getting a Place

To get the current place--in a format other than latitude and longitude and without having to use iOS’s reverse geocoding--you can make use of LocationKit’s requestPlace method handler, to provide more contextual location information, such as a venue or place:

locationManager.requestPlace { (place: LKPlacemark?, error: NSError?) -> Void in
  if let place = place {
    print("Welcome to \(place)")
  } else if error != nil {
    print("Uh oh, got an error: \(error)")
  } else {
    print("Your current place couldn't be determined")
  }
}

There are of course a lot more features on iOS you can tap into so please...

Continued on page 2. 

Doron Katz A keen passion for emerging technologies, practices and methodologies, Doron embraces the vision of lean development with continuous customer development. Consultant for various startups, as a Project and Product Manager, with a mobile engineering background in iOS, and over 10 years of professional web development experience.

Comments