The app provides the app with the ability to display your current location on the iphone
Current version of SDK 8.4 Xcode
Run Xcode Select Create a new Xcode project->single View application named Whereami
(1) Click on the folder Whereami, general->Linked frameworks and Libraries , "+" Search Corelocation.framework-&G T;add
(2) Open ViewController.h file, add the following code
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
@interface viewcontroller: uiviewcontroller <cllocationmanagerdelegate >{
iboutlet uitextfield *altitude;
iboutlet uitextfield *latitude;
iboutlet uitextfield *longitude;
Cllocationmanager *locmanager;
BOOL wasfound;
}
-(ibaction) Update: (ID) sender;
-(void) Locationmanager: (cllocationmanager *) Manager didupdatetolocation: (cllocation *) NewLocation fromlocation: (cllocation *) oldlocation;
-(void) Locationmanager: (cllocationmanager *) Manager didfailwitherror: (nserror *) Error;
@end
(3) Open viewcontroller.m file, add the following code
#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation Viewcontroller
-(ibaction) Update: (ID) sender{
Locmanager = [[cllocationmanager alloc]init];
[locmanager setdelegate:self];
[locmanager setdesiredaccuracy:kcllocationaccuracybest];
Locmanager. Distancefilter=ten;
nsstring *iosversion=[uidevice currentdevice]. Systemversion;
NSLog(@ "%@", iosversion);
if ((int) iosversion >= 8) {
[locmanager requestwheninuseauthorization]; allow access to location data in the course of the program (iOS8 positioning required)
}
[locmanager startupdatinglocation];
}
-(void) Locationmanager: (cllocationmanager *) Manager didupdatetolocation: (cllocation *) NewLocation fromlocation: (cllocation *) oldlocation{
if(wasfound)return;
WasFound = YES;
cllocationcoordinate2d loc = [newlocation coordinate];
latitude. Text = [nsstring stringwithformat:@ "%f", loc.latitude];
Longitude. Text = [nsstring stringwithformat:@ "%f", loc.longitude];
altitude. Text = [nsstring stringwithformat:@ "%f", NewLocation. Altitude];
}
-(void) Locationmanager: (cllocationmanager *) Manager didfailwitherror: (nserror *) error{
}
-(void) viewdidload {
[Super viewdidload];
additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
//Dispose of any resources, can be recreated.
}
@end
(3) Setting Info.plist
Click Info.plist, add Nslocationwheninuseusagedescription and nslocationalwaysusagedescription to the right
Set value to Yes
(4) UIView interface settings
Click Main.storyboard
Add three labels under Attributes, the Text is filled with "longitude", "latitude", "elevation";
Add three text field to display "longitude", "latitude", "elevation";
Right-click the Text field control to move the mouse over the circle behind "referencing Outlets"; The circle becomes (+); Drag the line to connect to "view Controller";
Release the mouse select key appears "Longitude", "latitude", "altitude"; Corresponds to "longitude", "latitude", "elevation" three text Field, select it separately.
Select: File--Save
Finally, in XCode, choose Build and then Running
(5) Real-machine commissioning
This article originated from the online blog tutorial, after I modified and tested. Original Blog Address http://blog.sina.com.cn/s/blog_5fae23350100e5fi.html
Four, the satellite positioning "Apple iOS example Programming Primer Tutorial"