Use GPS in IOS apps

Source: Internet
Author: User

Using GPS in IOS apps is roughly divided into the following two steps: 1. Adding corelocation. Framework; 2. Generating the cllocationmanager measurement position.

The test code is as follows:

// Locationviewctrl. h
# Import <uikit/uikit. h>
# Import <corelocation/corelocation. h>
@ Interface locationviewctrl: uiviewcontroller <cllocationmanagerdelegate> {
Cllocationmanager * Man;
}
@ Property (nonatomic, retain) cllocationmanager * Man;
@ End

Locationviewctrl. m
# Import "locationviewctrl. H"
# Import <corelocation/corelocation. h>

@ Implementation locationviewctrl
@ Synthesize man;

-(Void) viewdidload {
[Super viewdidload];
Man = [[cllocationmanager alloc] init];

// If you can use the local service
If ([Man locationservicesenabled]) {
// The instance that receives the event
Man. Delegate = self;
// Minimum distance between events (unspecified by default)
Man. distancefilter = kcldistancefilternone;
// Precision (best by default)
Man. desiredaccuracy = kcllocationaccuracybest;
// Start Measurement
[Man startupdatinglocation];
}
}

// If the function below the GPS measurement result is called
-(Void) locationmanager :( cllocationmanager *) Manager
Didupdatetolocation :( cllocation *) newlocation
Fromlocation :( cllocation *) oldlocation {

// Obtain the longitude and latitude
Cllocationcoordinate2d coordinate = newlocation. Coordinate;
Cllocationdegrees latitude = coordinate. Latitude;
Cllocationdegrees longpolling = coordinate. longpolling;
// Obtain the precision
Cllocationaccuracy horizontal = newlocation. horizontalaccuracy;
Cllocationaccuracy vertical = newlocation. verticalaccuracy;
// Get height
Cllocationdistance altitude = newlocation. altitude;
// Get time
Nsdate * timestamp = [newlocation timestamp];

// Output format in the following format: <latitude>, <longpolling> +/-<accuracy> m @ <date-time>
Nslog ([newlocation description]);

// Distance from the last measurement location
If (oldlocation! = Nil ){
Cllocationdistance d = [newlocation getdistancefrom: oldlocation];
Nslog ([nsstring stringwithformat: @ "% F", d]);
}
}

// If GPS measurement fails, the following function is called
-(Void) locationmanager :( cllocationmanager *) Manager
Didfailwitherror :( nserror *) error {
Nslog ([error localizeddescription]);
}
...

There are the following types of measurement accuracy: the higher the accuracy, the more power consumption.

Kcllocationaccuracynearesttenmeters 10 m
Kcllocationaccuracyhundredmeters 100 m
Kcllocationaccuracykilometer 1 km
Kcllocationaccuracythreekilometers 3 km

Because longitude and latitude cannot be set on the simulator, you can only test your GPS program on the actual device.

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.