Four, the satellite positioning "Apple iOS example Programming Primer Tutorial"

Source: Internet
Author: User
Tags uikit

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"

Related Article

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.