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.