CoreLocation positioning function developed by iPhone (6)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.