Posting a check-in to FourSquare

Now that we have created the basic module in order to authenticate against FourSquare, we are going to extend it in order to let the user "check-in" to a particular location. This works by sending details of your current place (for example, a bar, cinema, park, or museum) along with its latitude and longitude values to the FourSquare servers. From there, you can then tell which of your friends are nearby, or alternatively, make your location and activities public for everyone to see.

Note

Complete source code for this recipe can be found in the /Chapter 9/Recipe 4 folder.

How to do it…

Open your fsq_module.js file and extend the existing module so that it has the extra method as follows:

FOURSQModule.callMethod = function(method, GETorPOST, params, success, error) {
        //get the login information
        try {

            if (FOURSQModule.xhr == null) {
                FOURSQModule.xhr = Titanium.Network.createHTTPClient();
            }
            

            FOURSQModule.xhr.open(GETorPOST, FOURSQModule.API_URL + method + "?oauth_token=" + FOURSQModule.ACCESS_TOKEN);

            FOURSQModule.xhr.onerror = function(e) {
                Ti.API.error("FOURSQModule ERROR " + e.error);
                Ti.API.error("FOURSQModule ERROR " + FOURSQModule.xhr.location);
                if ( error ) {
                              error(e);
                       }
            };

            FOURSQModule.xhr.onload = function(_xhr) {
                Ti.API.debug("FOURSQModule response: " + FOURSQModule.xhr.responseText);
                if ( success ) {
                              success(FOURSQModule.xhr);
                        }
            };

            FOURSQModule.xhr.send(params);
        } catch(err) {
            Titanium.UI.createAlertDialog({
                title: "Error",
                message: String(err),
                buttonNames: ['OK']
            }).show();
        }
}; 

Now back in your app.js file, we are going to extend the "login" call we wrote in the previous recipe to now post a FourSquare check-in after a successful authorization:

FOURSQModule.init('yourclientid', 'http://www.yourcallbackurl.com'),
    
FOURSQModule.login(function(e){
    
       //checkin to a lat/lon location... you can get
       //this from a google map or your GPS co-ordinates
       var params = {
            shout: 'This is my check-in message!', 
            broadcast: 'public', 
            ll: '33.7,44.2'
        };
        
        FOURSQModule.callMethod("checkins/add", 'POST', params, 
        onSuccess_self, function(e) {
            Titanium.UI.createAlertDialog({
                title: "checkins/add: METHOD FAILED",
                message: e,
                buttonNames: ['OK']
            }).show();
        });


        //now close the foursquare modal window
        FOURSQModule.closeFSQWindow();


    }, 
    function(event) {
        Titanium.UI.createAlertDialog({
            title: "LOGIN FAILED",
            message: event,
            buttonNames: ['OK']
    }).show();

});

Now try running your app in the emulator. After logging into the FourSquare system, you should automatically have posted a test check-in titled "This is my check-in message!" and the FourSquare system should send you a successful response message and log it to the console.

How it works…

The callMethod() function of our FourSquare module does all of the work here. It is essentially taking in the method name to call, along with whether it is a GET or POST call and the parameters required to make that method work. Our example code is calling the checkins/add method, which is a POST, and passing it through the parameters of shout, broadcast, and ll. These are our message, privacy setting, and current location respectively. All of the authorization work, including saving our access token, is done via the previous recipe. The following console output shows our response from FourSquare after a successful checkin post:

[DEBUG] destroyAuthorizeUI

[DEBUG] destroyAuthorizeUI:webView.removeEventListener

[DEBUG] destroyAuthorizeUI:window.close()

[DEBUG] FOURSQModule response: {"notifications":[{"type":"notificationTray","item":{"unreadCount":0}},{"type":"message"
,"item":{"message":"OK, got your shout (This is my check-in message!)!"}}],"response":{"checkin":{"i
d":"4ebf9a5d7ee54e4cd299b72e","createdAt":1321179741,"type":"shout","shout":"This is my check-in mes
sage!","timeZone":"Asia/Baghdad","location":{"lat":33.7,"lng":44.2}}}}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43