Use MongoDB to make your app location-aware

Crunching contextual data about users can sell more stuff -- and in a mobile world, location data rules. Here's how to add location awareness to mobile apps with MongoDB

Page 2 of 2

To start, I pulled all the neighborhood boundary data from the city of Chicago's website. After a quick conversion to GeoJSON, I imported the data into a collection in MongoDB named neighborhoods. Each neighborhood document looks roughly like this:

{

"_id" : ObjectId("5265a131089bf0213b4c307c"),

"type" : "Feature",

"properties" : {

"PRI_NEIGH" : "West Loop",

...

},

"geometry" : {

"type" : "Polygon",

"coordinates" : [

[

[

-87.64446845179363,

41.85995449225369

],

...

]

}

}

While some geospatial queries in MongoDB require a geospatial index, $geoIntersects does not. That said, it's smart and easy to add indexes for common queries, so I did.

Now the query itself is trivially simple. To find out which neighborhood I'm in, I'll build a query like this:

db.neighborhoods.find({

geometry: {

$geoIntersects : {

$geometry: {

type: "Point",

coordinates: [ lng, lat ]

}

}

}

});

What does it look like if I query for all the neighborhoods that my bike route passes through?

db.neighborhoods.find({

geometry: {

$geoIntersects : {

$geometry: {

type: "LineString",

coordinates: [ [ lng0, lat0], [ lng1, lat1 ], [ lng2, lat2], ... ]

}

}

}

});

It's almost exactly the same and produces the results we want.

The whole process to download, convert, import, and query this information took less than an hour, leaving me the rest of the day to build up a simple back end and work on the UI. This sort of development speed is powerful, enabling quick prototyping and experimentation at low cost.

The app I've built is obviously geocentric in order to clearly outline some of the possibilities of this new query. When it comes to contextualizing your user experience, however, you don't have to make it this obvious. In fact, you probably won't want to. One of the most powerful aspects of a contextualized experience is that the lines blur between your app and the other aspects of the user's life. An example of this is in iOS 7, where, based on your current location and location history, you'll find out how long it will take to get to your next likely destination in the notification center.

With these sorts of great tools at your disposal, it's time to explore the possibility of incorporating into your application. There's a low cost of experimentation, and the potential for finding interesting and beneficial applications of the technology is high. So play around with it a little and see what happens, your customers (and your bottom line) will be happier for it.

The source code for the app is available on GitHub, and a running version of the app can be found here.

This article, "Use MongoDB to make your app location-aware," was originally published at InfoWorld.com. Keep up on the latest developments in application development, and read more of Andrew Oliver's Strategic Developer blog at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.

To comment on this article and other InfoWorld content, visit InfoWorld's LinkedIn page, Facebook page and Twitter stream.
| 1 2 Page 2
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.