How can I obtain the coordinates of my current position on the iPhone?
1. First
# Import<Corelocation/corelocation. h>
2. Two variables are defined in the class:
Cllocationmanager*Locationmanager;
Cllocationcoordinate2dCurlocation;
3. Class implementationCllocationmanagerdelegate proxy.
4. ImplementationCode:
// Obtain your current location information <br/>-(void) getcurposition <br/>{< br/> // start to test your location <br/> If (locationmanager = nil) <br/>{< br/> locationmanager = [[cllocationmanager alloc] init]; <br/>}</P> <p> If ([cllocationmanager locationservicesenabled]) <br/> {<br/> locationmanager. delegate = self; <br/> locationmanager. desiredaccuracy = kcllocationaccuracybest; <br/> locationmanager. distancefilter = 10.0f; <br/> [locationmanager startupdatinglocation]; <br/>}< br/>
Startupdatinglocation is the start of the search for the current location. As long as the range of the current location movement exceeds the range specified by distancefilter, the system will receive
The latest coordinate position information.
To disable location update, you must call stopupdatinglocation.
5. If you get the current user location, there will be a delegate function:
-(Void) Locationmanager :(Cllocationmanager*) Manager didupdatetolocation :(Cllocation*) Newlocation
Fromlocation :(Cllocation*) Oldlocation
Called.
// Respond to the update of the current location. Record the latest location here <br/>-(void) locationmanager :( cllocationmanager *) manager didupdatetolocation :( cllocation *) newlocation <br/> fromlocation :( cllocation *) oldlocation <br/>{< br/> nslog (@ "newlocation: % @", [newlocation description]); </P> <p> // Save the new location <br/> curlocation = newlocation. coordinate; <br/>}