Wednesday, June 27, 2012

How to develop an IPhone Location App


You can say the continuation to my prev article on how to use the HTML5 GeoLocation api, but its different this time. It uses Objective C to get the location details rather than using HTML5.

Is building IOS App easy ?

I asked my self this question many times with out knowing how easy it is  before I attended the work shop conducted by Dr.Barbara Hecker.

My 4th App (other than my 3 hello world app) I created this Location Detection app which detects your position and displays latitude & longitude on the iPhone screen.

All you need is  Xcode (I used Xcode Version 4.3.3), IOS 5 SDK, iPhone Simulator and little bit of scripting knowledge :)

For who know how to create an app this should be simple.

Create new project using a Single View Application. This will create a default ViewController Interface & Implementation class (ViewController.h and ViewController.m). Download the code from here

To implement the location interface all you need to know about is LocationManager in IOS. This CLLocationManage is the interface to deliver the location events. The main objective is to deliver event, create instance and attach a delegate (CLLocationManageDelegate) object to it.
For more info on LocationManager refer this link.

In this I used the below properties
1) delegate property for configuring the updates
2) startUpdatingLocation to start the updates 

Make sure you import CoreLocation library (Target > Build Phases > Add CoreLocation under Link binary with libraries. It took me quite a while to figure it out properly. If you get "undefined symbols for architecture i386 _OBJC_CLASS_$_CLLocationManager" don't be panic)

Cut to chase, below is the sample implementation details.

//  ViewController.h
#import
#import
@interface ViewController : UIViewController

<CLLocationManagerDelegate
{
    IBOutlet UITextField *latitudeText;
    IBOutlet UITextField *longitudeText;
    CLLocationManager *locMgr;
}

@property (retain,nonatomic) IBOutlet UITextField *latitudeText;
@property (retain,nonatomic) IBOutlet UITextField *longitudeText;

@property (weak, nonatomic) IBOutlet UIButton *locationDetect;

@end

Using the story board I added 2 text fields (for latitude & longitude) and control+click on the textFields and drag it to interface class. This will create property for each text fields like below

@property (retain,nonatomicIBOutlet UITextField *latitudeText;

Also I created button, which on click will get the location.

Below is my implementation class

//  ViewController.m
#import "ViewController.h"
@implementation ViewController

@synthesize latitudeText,longitudeText,locationDetect; 
CLLocationManager *locMgr;

......
- (void)viewDidLoad
{
    [super viewDidLoad];
    locMgr = [[CLLocationManager alloc]init];
    [locMgr startUpdatingLocation];
}

......

- (IBAction)locationDetect:(id)sender {
    NSString *latitudeTextData = [[NSString
                                   alloc]initWithFormat:@"%g",locMgr.location.coordinate.latitude];
    NSString *longitudeTextData =[[NSString alloc]
                                  initWithFormat:@"%g",locMgr.location.coordinate.longitude];
    self.latitudeText.text = latitudeTextData;
    self.longitudeText.text = longitudeTextData;
}

@end

In this I created a LocationManager object and started the location update process by using the startUpdatingLocation method.  Then capture the values from the location manager and update the text field.

Isn't quite simple ;)







Wednesday, April 18, 2012

GeoLocation API HTML5

Finding the user location with HTML5 GeoLocation API is quite simple. I have been experimenting Geo api, just sharing what I learned. Follow the reference given below for more understanding.

Geolocation can be obtained from navigator object like navigator.geolocation.

First step check whether your browser supports geolocation. If not fall back to native javascript.

if(navigator.geolocation)
else alert("your browser doesn't support geolocation")

geolocation object has 3 methods

  • getCurrentPosition
  • watchPosition
  • clearWatch


getCurrentPosition & watchPosition looks similar and takes same arguments.

ex:

navigator.geolocation.getCurrentPosition(success, error, {map of geolocation options});

success = function(position) {
position.coords.latitude ;
position.coords.longitude  ;
position.coords.accuracy;
};

failure = function(error) {
// 3 types of error

1: 'Permission denied',
2: 'Position is not available',
3: 'Request timeout'

};

Failure return error code  of 1,2,3.


The third argument for both getCurrentPosition and watchPosition is the geolocation options which is optional.
 1) enableHightAccuracy - takes boolean true/false, enables the device to get more accurate latitude & longitude positions
2) timeout - time allowed for the device to get the data. keep it zero will let the browser to never timeout (in milliseconds). Setting to 2000 will wait for 2000 milliseconds before it quits with REQUEST TIME OUT error code 3.
3) maximumAge - lets the browser to use the cached data. if set to zero will always look up new position for each request.

I have made a small demo which is still in work in progress.

Reference:
Geolocation API Specification
HTML5 By Bruce Lawson and Remy Sharp

Friday, April 06, 2012

Boolean Attribute vs Boolean Value

If you are writing some thing using HTML5 then you need to understand their Boolean Attributes which is not same as what you think as true or false.Thanks to the Boolean father George Boole

Boolean attribute doesn't need any value set as true or false,  require=required.
Then how to use Boolean Attribute in your HTML ?
just add this attribute and do not assign any value. What .. Yes, its up to you if you want to assign you can.

Lets see the below example.
< input type=text name="user" disabled / >

You can use disabled=disabled or disabled=true.  Both has same meaning or just keep it simple and use "disabled" as I said the very existence of them makes meaning KISS principle

List of HTML5 Boolean Attributes