views:

1355

answers:

3

How do you get the current location of iPhone through code? I need to track the location using GPS.

+1  A: 

You're looking for the "CoreLocation" API.

Here's a tutorial

David Gelhar
Thanks David...
Dinesh Kumar
+1  A: 

This page on CLLocationManager has five source code examples

TechZen
Thanks TechZen...
Dinesh Kumar
+3  A: 

There is (as always for core functionality) comprehensive documentation on the developer site and you may find this example useful;

The key points to remember are;

1) Once you start receiving location updates, they arrive asynchronously and you need to respond to them as and when they come in. Check the accuracy and timestamps to decide if you want to use the location or not.

2) Don't forget to stop receiving updates as soon as you have the location you want (this might not be the first one), otherwise you will cane the battery life.

3) The first location returned to you is often the LAST CACHED one, and the phone will report this location with the same (possibly high) accuracy it had when it got the fix before. So you might get a fix that claims to be accurate to 10 metres but it was actually received yesterday when you were miles away from your current location. The motto of this is DON'T FORGET TO CHECK THE TIMESTAMP - this tells you when the location fix was actually received.

Hope that helps.

Roger
Thanks Roger,Will catch u after trying with your example
Dinesh Kumar