After learning the iPhone's CoreLocation, I think about Android's positioning and development, which saves a lot of trouble. The iPhone encapsulates the positioning function development module well and takes only a few steps, you can obtain multiple parameters, such as the location of the device!
1. Start XCode4.3.2, click the menu item File> New> Project..., create a Project in the Sigle View Application template, and name it WhereAmI:
2. Click the ViewControler. h header file. Because the CoreLocation framework does not belong to the UIKit framework, you need to introduce it and add the Protocol:
[Plain]
# Import <UIKit/UIKit. h>
# Import <CoreLocation/CoreLocation. h>
@ Interface ViewController: UIViewController <CLLocationManagerDelegate>
{
CLLocationManager * locationManager;
CLLocation * startPoint;
UILabel * latLabel;
UILabel * lonLabel;
UILabel * distance;
}
@ Property (retain, nonatomic) CLLocationManager * locationManager;
@ Property (retain, nonatomic) CLLocation * startPoint;
@ Property (retain, nonatomic) IBOutlet UILabel * latLabel;
@ Property (retain, nonatomic) IBOutlet UILabel * lonLabel;
@ Property (retain, nonatomic) IBOutlet UILabel * distance;
@ End
3. Click the ViewControler. m file and add the following code:
[Plain]
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
@ Synthesize startPoint;
@ Synthesize locationManager;
@ Synthesize latLabel;
@ Synthesize lonLabel;
@ Synthesize distance;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. locationManager = [[CLLocationManager alloc] init];
Self. locationManager. delegate = self;
Self. locationManager. desiredAccuracy = kCLLocationAccuracyBest;
[LocationManager startUpdatingLocation];
}
-(Void) viewDidUnload
{
[Super viewDidUnload];
// Release any retained subviews of the main view.
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}
# Pragma mark-
# Pragma mark CLLocationManagerDelegate Methods
-(Void) locationManager :( CLLocationManager *) manager didUpdateToLocation :( CLLocation *) newLocation fromLocation :( CLLocation *) oldLocation
{
If (startPoint = nil)
StartPoint = newLocation;
// Longitude
NSString * lon = [[NSString alloc] initWithFormat: @ "% g", newLocation. coordinate. longpolling];
Self. lonLabel. text = lon;
[Lon release];
// Latitude
NSString * lat = [[NSString alloc] initWithFormat: @ "% g", newLocation. coordinate. latitude];
Self. latLabel. text = lat;
[Lat release];
// Calculate the moving distance
CLLocationDistance ld = [newLocation distanceFromLocation: startPoint];
NSString * distanceString = [[NSString alloc] initWithFormat: @ "% gm", ld];
Self. distance. text = distanceString;
[DistanceString release];
}
-(Void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error
{
NSString * errorType = (error. code = kCLErrorDenied )? @ "Access Denied": @ "Unkown Error ";
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Error getting location" message: errorType delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
[Alert show];
[Alert release];
}
@ End
4. Run:
Author: js_dada