Geolocation with Kii Cloud on iOS

Kii Cloud has recently released geolocation for its iOS SDK (if you’re an android dev, this is also available – see our post here).

One of the most useful features of any smartphone is the fact that the device is location-aware. This gives developers the opportunity to customize the user’s experience based on where they are and what is around them.

Why Geolocation?

Whether your users are checking-in, saving a favorite landmark or searching for the best restaurant around, your app needs to be able to handle geographic locations (generally stored as 2D coordinates). Surely, you could store these as regular doubles or floats, but what happens when you want to query based on distance between coordinates? Your app would need a layer to handle this type of query by pulling coordinates and calculating distances before returning the objects match the constraints.

Fortunately, this type of search functionality is already baked into Kii Cloud!

How to use

You can utilize geolocation functionality within Kii Cloud by using extensions of the objects you’re already used to using! You can save KiiObjects with GeoPoints attached to them and even query against objects within a certain radius or even a specific bounding box. A great feature unique to Kii Cloud is that you can append multiple GeoPoints to the same KiiObject, as if you were adding a standard key/value pair!

Check out the examples below or head over to our comprehensive guides to learn more.

Creating objects with geopoints

Attach a location to a standard KiiObject by appending a GeoPoint attribute as shown:

// create a geopoint
KiiGeoPoint *point = [[KiiGeoPoint alloc] initWithLatitude:USER_LATITUDE
                                              andLongitude:USER_LONGITUDE];

// create a kiiobject
KiiObject *obj = [[[KiiUser currentUser] bucketWithName:CHECKIN_BUCKET] createObject];

// add the geopoint to the object
[obj setGeoPoint:point forKey:LOCATION_KEY];

// save it to the backend
[obj saveWithBlock:^(KiiObject *object, NSError *error) {
    // do something
}];

Querying for objects based on location

Query for objects with location attributes within a certain radius by using the code below. This particular case could be useful in finding landmarks saved by the user within 100km of their current location. This can be done by appending a clause to a KiiQuery that handles GeoPoints as shown:

        
// get the user's location and use it as our center point
KiiGeoPoint *center = [[KiiGeoPoint alloc] initWithLatitude:LATITUDE
                                               andLongitude:LONGITUDE];

// create the clause for our query
// we want to get all checkins within 100km of the user's current location
KiiClause *clause = [KiiClause geoDistance:LOCATION_KEY
                                    center:center
                                    radius:100000 // within 100km
                           putDistanceInto:nil];

// create the query
KiiQuery *query = [KiiQuery queryWithClause:clause];

KiiBucket *bucket = [[KiiUser currentUser] bucketWithName:CHECKIN_BUCKET];

// execute the query asynchronously
[bucket executeQuery:query
           withBlock:^(KiiQuery *query, KiiBucket *bucket, NSArray *results, KiiQuery *nextQuery, NSError *error) {
    // do something           
}];

Sample Application

We have put together a small sample application to show how easy it is to get started with geolocation in Kii Cloud. It provides a mechanism for users to save and retrieve points of interest based on their location, displaying them all within a MKMapView.

Check out the sample code in our GitHub project page

Geolocation sample iOS

Next Steps

Kii Cloud makes it very easy to bring location awareness to your application. If you would like to get more detailed information, head over to our guides and reference sections to learn more.

- Share -

Chris Beauchamp

More posts from

  • Kii IoT platform enables customers to create smart, connected solutions and services
  • Long track record of supporting customers like Docomo, Toshiba, Kyocera and many others
  • Strong ecosystem partners across the globe, consisting of carriers, device/sensor manufacturers, system integrators, network/infrastructure providers, chip vendors and solution developers
  • HQ in Tokyo, US HQ in Silicon Valley & offices in China, Taiwan, Hong Kong and Europe (UK, Spain, Germany)
  • www.kii.com