Editor’s Note: This post is cross-posted with permission from the Clearly Innovative blog, maintained by Aaron Saunders.
In Part Two of the Stackmob Appcelerator blog posting, we are going to quickly show how to use the module to make geospatial queries. We will also include the introduction of a
login method that was added to the client along with the exposing of the
headers property as a parameter to be passed into the client API
To keep things simple, I will be updating the objects that were used in part one of this blog post. See this ( Titanium Appcelerator Quickie: StackMob API Module Part One )
You can see in the image below, the new location field that was added to the user object. This location field has a lat lon field that will be used for the geospatial queries.
Now lets add some location specific information to your object, set the values for lat lon for searching
Here is a gist that has some sample data that was used for this posting
This is what the query looks like when searching for data near a location. Notice the calculation into radians, the query requires the distance to be in radians.
For example, 10 miles in radians would be 10 / 3963 = 0.0025233409
This is what the query looks like when searching for data within a bounding box. In this example you need to set the bounding box coordinates of the bottom left and top right.
Login and Logout
The module has also been updated with the
login method to make is simple to authenticate a user
Logging the current user out
Introduction of Headers Property
Why is this important? Because the headers do all the magic for sorting, paging and querying data. All of the complexities of query formats are not covered in this post, but can be reviewed in the Stackmob Documentation
The complete documentation on sorting and paging can be found on the Stackmob API Documentation Page
Get all User(s) that have a zip code value and sort results by the zip field ascending, search for zip greater that blank field.
Notice the use of the
params property to implement the query, and the header to sort the information.
Using the same headers property, we can ‘page’ through the objects by specifying the range of objects we want returned.
- Titanium Appcelerator Quickie: StackMob API Module Part One
- Titanium Appcelerator Quickie: StackMob API Module Part One – A
- StackMob Website
- StackMob API Documentation
- Appcelerator Developer
- Original Zipped Project Used in the blog posting
- Download module: stackmob-module.min.js