iOS API Tutorials
Tutorial 4 — Adding Buttons
Adding two UI buttons to the mapping engine demo
If you would like to experiment with or "play with" the BA3 Altus Mapping Engine™, it would be nice to have a quick and easy way to add UI buttons. You can of course do this in the UI builder in Xcode, but the method here is quicker and therefore lends itself to simple experiments. In this tutorial we will demonstrate how to add two buttons — one to turn the GPS functionality on and off, the other to turn the Track-Up capability on and off. Start with the code from Tutorial 3.
Step 1
Start by adding several lines of code to ViewController.h
These lines add a property to keep track of whether the GPS is currently on or off, add two properties for the two buttons we will add, and add two methods that will be called when the buttons are clicked by the user.
Step 2
Add a function to ViewController.m to add the two buttons:
We are not using the UI builder at all — we are creating, labeling and positioning the buttons ourselves here in the code. This has the advantage of working identically on the iPhone and iPad and handling rotation automatically, which is great for keeping a tutorial short and simple. What we give up is beauty.
Step 3
Add this line to the viewDidLoad function in ViewController.m prior to calling initializeMappingEngine:
Step 4
We need to add functions to deal with the buttons being clicked by the user. Add these functions to ViewController.m:
Step 5
We need to add functions to deal with the orientation of the device. We have ignored this in the prior tutorials but will deal with it here. Add these functions to ViewController.m:
Step 6
Run the app. The buttons should work as expected when you click them. In the iOS Simulator menu bar, choose the Debug menu and then choose the Location item. Choose one of the options that show motion, like "Freeway Drive" or "City Bicycle Ride". Look in the console window of Xcode and you will see the speed and course changing. The map should pan to the new location so that the new location is centered on the screen and rotate the map to demonstrate Track-Up mode if you click the buttons to turn on the GPS and turn on Track-Up.
Step 7
If you get lost or wish to save yourself the typing, simply checkout tutorial4 using these commands:
git reset --hard
git checkout tutorial4
Step 8
Now we would like to add a marker to show the current GPS location and orientation in the app. This is explained in Tutorial 5.
You can learn more about the Altus Mapping Engine™ classes used in this tutorial in the Documentation. If you have any questions about the Altus Mapping Engine™, any feature requests or suggestions for improving the Altus Mapping Engine™, please send them to [email protected].