Lab Session: Play with Features

From NSB App Studio
Revision as of 06:24, 7 January 2014 by Admin (talk | contribs) (8 revisions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In this lab, you will add a compass to your application, allow it to pull weather data by your location, and have it choose a random location on shake.

Student Outline

The following lab contains three parts. Start by opening LabFeatures2.nsx.

Compass

Using the resources and code from the Compass sample, add a compass to frmCompass in your app.

Geolocation

  1. Attach an onchange event to flpCurrentLocation. When it is switched to On, you should retrieve the current location using [navigator.geolocation.getCurrentPosition] and disable txtCity and btnLoad. When switched to off, txtCity and btnLoad should be re-enabled.
  2. When the current location has been retrieved, enable btnLoad.
  3. When btnLoad is pressed and flpCurrentLocation is in the On position, load the weather information via latitude and longitude, using the following API: http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139

Accelerometer

  1. A shaking motion can be sensed by setting a threshold for change in accelerationIncludingGravity. Experiment to determine what an acceptable threshold is for your device. (an example of this in JavaScript is available here: http://stackoverflow.com/a/4475330)
  2. When on frmMain, listen for accelerometer events that could indicate a shaking motion. Set the location to a random latitude and longitude to load weather for that location.

Bonus

  • Modify the compass so it points somewhere other than north.
  • As you saw in the lecture, it's possible to update the current location continuously. Modify your code to function this way, rather than only retrieving the location once.
  • When retrieving a random location, it might be helpful to display what cities you are near, or what the latitude and longitude is. Modify your app to provide this functionality.
  • The accelerometer is capable of detecting many movements beyond shakes. Now that you've modified your app to display the random location you've retrieved, modify it so that after a random location is retrieved tilting the phone will modify that location, increasing or decreasing the latitude or longitude.