Book Image

PhoneGap Mobile Application Development Cookbook

By : Matt Gifford
Book Image

PhoneGap Mobile Application Development Cookbook

By: Matt Gifford

Overview of this book

<p>As more and more of our online life and daily activities evolve into the mobile landscape it has never been of more importance to start venturing into the world of mobile applications.<br />&nbsp;<br />With PhoneGap, you can put your existing development skills and HTML, CSS, and JavaScript knowledge to great use by creating mobile apps for cross-platform devices.<br /><br />"PhoneGap Mobile Application Development Cookbook" will show you how to create native mobile applications for multiple device platforms using the PhoneGap API. You will learn to build dynamic applications that interact with the native functionality on the device to create a rich, interactive experience for your users.<br /><br />This is a cookbook with each section written as a recipe in an informal, friendly style. Each recipe contains the complete code needed to build your applications, and plenty of screenshots showing the completed projects running on devices are included to assist you.<br /><br />"PhoneGap Mobile Application Development Cookbook" will introduce the PhoneGap API to you in clear, concise sections, breaking down each recipe to achieve realistic working projects that can be applied to your own mobile applications.<br /><br />You will be shown how to use the mobile development framework to build applications that can be deployed and run on multiple mobile platforms including iOS, Android, and BlackBerry.<br /><br />This book will explore the various methods and functions available using the API, and how to write code that interacts with the native functions, including geolocation sensors, contacts database, and native button events of the device to create powerful applications ready for the market place.<br /><br />"PhoneGap Mobile Application Development Cookbook" will help you break into the world of mobile application development. Build mobile applications in code you&rsquo;re comfortable with, create a richer user experience for your users and become part of the ever-growing mobile market place.</p>
Table of Contents (17 chapters)
PhoneGap Mobile Application Development Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a visual compass to show the devices direction


The PhoneGap API provides developers with the ability to receive coordinate and heading information from the device. We can use this information to build a custom compass tool that responds to the device movement.

How to do it...

  1. Create the HTML layout for our page, including the cordova-2.0.0.js reference.

    <!DOCTYPE html>
    <html>
      <head>
         <meta name="viewport" content="user-scalable=no, 
                initial-scale=1, maximum-scale=1, 
                minimum-scale=1, width=device-width;" />
    
        <title>Compass</title>
        <script type="text/javascript" 
            src="cordova-2.0.0.js"></script>
      </head>
      <body>
    
      </body>
    </html>
  2. In this example, we will be referencing certain elements within the DOM by class name. For this we will use the XUI JavaScript library (http://xuijs.com/). Add the script reference within the head tag of the document to include this library.

  3. Let's also create the script tag block that will hold our custom JavaScript for interaction with the PhoneGap API, as shown in the following code:

    <head>
       <meta name="viewport" content="user-scalable=no, 
                initial-scale=1, maximum-scale=1, 
                minimum-scale=1, width=device-width;" />
    
      <title>Compass</title>
      <script type="text/javascript" 
          src="cordova-2.0.0.js"></script>
    
      <script type="text/javascript" 
          src="xui.js"></script>
    
    
      <script type="text/javascript">
      // PhoneGap code will go here
    
      </script>
    
    </head>
  4. Add a new div element within the body tag and give this the class attribute of container. This will hold our compass elements for display.

  5. The compass itself will be made up of two images. Both images will have an individual class name assigned to them, which will allow us to easily reference each of them within the JavaScript. Add these two within the container element.

  6. Next, write a new div element below the images with the id attribute set to heading. This will hold the text output from the compass response:

    <body>
    
      <div class="container">
    
          <img src="images/rose.png" 		
            class="rose" width="120" height="121" 
            alt="rose" />
          
          <img src="images/compass.png" 	
            class="compass" width="200" height="200" 
            alt="compass" />
    
        <div id="heading"></div>
      
      </div>
    
    </body>
  7. With the initial layout complete, let's start writing our custom JavaScript code. First, let's define the deviceready event listener. As we are using XUI, this differs a little from other recipes within this chapter:

    Var headingDiv;
    
    x$(document).on("deviceready", function () {
    
    });
  8. When we have a result to output to the user of the application, we want the data to be inserted into the div tag with the heading id attribute. XUI makes this a simple task, and so we'll update the headingDiv global variable to store this reference:

    x$(document).on("deviceready", function () {
      headingDiv = x$("#heading");
    
    });
  9. Let's now include the requests to the PhoneGap compass methods. We'll actually call two requests within the same function. First we'll obtain the current heading of the device for instant data, then we'll make a request to watch the device heading, making the request every tenth of a second thanks to the use of the frequency parameter. This will provide use with continual updates to ensure the compass is correct:

    navigator.compass.getCurrentHeading(onSuccess, onError);
    navigator.compass.watchHeading(
                  onSuccess, onError, {frequency: 100});
  10. Both of these requests use the same onSuccess and onError method to handle output and data management. The onSuccess method will provide us with the returned data in the form of a heading object.

  11. We can use this returned data to set the HTML content of the heading element with the generated message string, using the headingDiv variable we defined earlier.

  12. Our visual compass also needs to respond to the heading information. Using the CSS method from XUI, we can alter the transform properties of the rose image to rotate using the returned magneticHeading property. Here we reference the image by calling its individual class name, .rose:

    // Run after successful transaction
    // Let's display the compass data
    function onSuccess(heading) {
      headingDiv.html(
        'Heading: ' + heading.magneticHeading + '° ' + 
        convertToText(heading.magneticHeading) + '<br />' + 
        'True Heading: ' + heading.trueHeading + '<br />' +
        'Accuracy: ' + heading.headingAccuracy
    	);
          
      // Alter the CSS properties to rotate the rose image
      x$(".rose").css({
        "-webkit-transform": 
        "rotate(-" + heading.magneticHeading + "deg)",
         "transform": 
        "rotate(-" + heading.magneticHeading + "deg)"
      });
          
    }
  13. With the onSuccess handler in place, we now need to add our onError method to output a user-friendly message, should we encounter any problems obtaining the information, as shown in the following code:

    // Run if we face an error 
    // obtaining the compass data
    function onError() {
      headingDiv.html(
        'There was an error trying to ' + 
        'locate your current bearing.'
      );
    }
  14. When creating our message string in the onSuccess function we made a call to a new function called convertToText. This accepts the magneticHeading value from the heading object and converts it into a text representation of the direction for display. Let's include this function outside of the XUI deviceready block:

    // Accept the magneticHeading value
    // and convert into a text representation
    function convertToText(mh) {
      var textDirection;
      if (typeof mh !== "number") {
        textDirection = ''; 
      } else if (mh >= 337.5 || (mh >= 0 &&  mh <= 22.5)) {
        textDirection =  'N'; 
      } else if (mh >= 22.5 && mh <= 67.5) {
         textDirection =  'NE'; 
      } else if (mh >= 67.5 && mh <= 112.5) {
         textDirection =  'E'; 
      } else if (mh >= 112.5 && mh <= 157.5) {
         textDirection =  'SE'; 
      } else if (mh >= 157.5 && mh <= 202.5) {
         textDirection =  'S'; 
      } else if (mh >= 202.5 && mh <= 247.5) {
         textDirection =  'SW'; 
      } else if (mh >= 247.5 && mh <= 292.5) {
         textDirection =  'W'; 
      } else if (mh >= 292.5 && mh <= 337.5) {
         textDirection =  'NW'; 
      } else {
        textDirection =  textDirection;
      }
      return textDirection;
    }
  15. Let's provide some CSS to position our two images on the screen and ensure the rose image is overlaying the compass image. Create a new file called compass_style.css and insert the following styles into it:

    .container {
      position: relative;
      margin: 0 auto;
      width: 200px;
      overflow: hidden;
    }
    
    #heading {
      position: relative;
      font-size: 24px;
      font-weight: 200;
      text-shadow: 0 -1px 0 #eee;
      margin: 20px auto 20px auto;
      color: #111;
      text-align: center;
    }
    .compass {
      padding-top: 12px;
    }
    .rose {
      position: absolute;
      top: 53px;
      left: 40px;
      width: 120px;
      height: 121px;
    }
  16. Finally, include the reference to the compass_style.css file in the head tag of the HTML document:

    <title>Compass</title>
    <link href="compass_style.css" 
        rel="stylesheet" />
    <script type="text/javascript" 
        src="cordova-2.0.0.js"></script>
    <script type="text/javascript" 
        src="xui.js"></script>
  17. Running the application on the device, the output will look something like the following screenshot:

How it works...

The watchHeading method from the PhoneGap API compass functionality retrieves periodic updates containing the current heading of the device at the interval specified as the value of the frequency variable passed through. If no interval is declared, a default value of 100 milliseconds (one-tenth of a second) is used.

With every successful request made on the continuous cycle, the onSuccess method is executed and formats the data for output onto the screen, as well as making a change to the transform property of the graphical element to rotate in accordance with the heading.

The onSuccess method returns the obtained heading information in the form of the compassHeading object, which contains the following properties:

  • magneticHeading: A Number value ranging from 0 to 359.99 that specifies a heading in degrees at a single moment in time.

  • trueHeading: A Number value ranging from 0 to 359.99 that specifies the heading relative to the geographic North Pole in degrees.

  • headingAccuracy: A Number value that indicates any deviation in degrees between the reported heading and the true heading values.

  • timestamp: The time in milliseconds at which the heading was determined.

See also